0
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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 $$

$\endgroup$
0
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Note that my answer only addresses the mathematical part of your question. Depending on your needs, there are most likely better solutions to your problem and you may want to ask over at stackexchange if someone can guide you in the right direction. $\endgroup$ – Stefan Mesken Aug 10 '16 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.