# Calculate how much I have to reduce a picture to reach a given size

I have an application where I need to resize the users picture to max. 50KB

To resize I use a Method that sizes the picture to a given Width/Height.

The picture is a Bitmap with max. 8BPP. (Width * Height * 8) = Size in Bits

What I need to know now is: How can I calculate the amount of pixels I need to cut off to reach a maximum size of 50 KB. The result should be as close to the maximum as possible. (Not just cut off more than enough).

There's a tolerance of some Kilo Bytes but not too much.

Assuming you want to keep the aspect ratio. Define $r = w_{original}/h_{original}$.
You want to find $w$ and $h$ such that : $$w\cdot h \cdot 8 \leq 50\cdot 1024 \cdot 8\\ w/h = r$$ Inserting the second into the first : $$rh^2 \leq 51200 \implies h=\sqrt{51200/r} \\ w = r\cdot h$$
Let $r$ be the ratio of width/height of the original picture - you want to preserve that. Now you just have to solve $$y \cdot(1+r) = 50 \cdot 1024 \cdot 8.$$ Then $\underline{y}$ is the height of the resized picture in pixels and, where $\underline{y}$ is the maximal integer less than or equal to $y$ and setting $x := y \cdot r$, $\underline{x}$ is the height of the resized picture.