开发者

Finding a percentage

开发者 https://www.devze.com 2023-01-27 15:33 出处:网络
Here\'s the problem. I have a picture that will have a different Height every time according to the picture that\'s loaded.

Here's the problem. I have a picture that will have a different Height every time according to the picture that's loaded.

I'd like to scale that picture so it fits inside of the document which has 800 pi开发者_Python百科xels height. So if the image has 2560px in height, I need to find out the PERCENTAGE needed to bring that pixel height down to 750 so it fits snuggly on the page.

I'm stuck trying to find the formula for this simple enough problem.

Here's my code:

iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(
    image, System.Drawing.Imaging.ImageFormat.Jpeg);

if (pic.Height > pic.Width)
{
    //Maximum height is 800 pixels.
    pic.Height formula goes here....
}
else
{
    //Maximum width is 600 pixels.
    pic.Width formula goes here....
}


Some number p is such that p * 2560 = 750. Therefore, p = 750 / 2560 = 0.29296875.

Of course, make sure that you do floating-point division.


The rule of three will help you sort it out.


I don't know if I understand your problem exactly. Do you mean something like this?

percentage = (frameHeight / picHeight) * 100

Example:

(750 / 2560) * 100 = 29

That means: 2560 * 0.29 = 750


Here x is the maximum desired height, y is the actual image height and p is the percentage.

p = x / y;
x = p * y;
y = x / p;

Given any two you can find the other.

0

精彩评论

暂无评论...
验证码 换一张
取 消