Looking on the web there are many, many people trying to sort out how to overcome the INT rounding in programming and now I am one of the many.

Having said that there are lots of seemingly good looking answers to the problem, I have not found one that works for me.

I am trying to automatically resize photos and need the ratio to be precise to 2 decimal places, i.e.

Original image is 3000 W x 2400 H and the application resizes from the longest side, 3000, down to 800.

So 3000/800 = 3.75 then it works on the shorter side by applying the outcome of the first equation as the divisor thus, 2400/3.75 = 640.

Sounds really simple only it doesn't work.

It rounds the 1st equation down to 3 so the resized image becomes 800x800, not correct.

Here is a snippet of my code below:-

origW - original width
origH - original height

if (origW > origH){
					int RorigW = origW/800;
					int RorigH = origH/RorigW;

Any help would be lovely, I guess it is probably really simple because all the photo imaging software use resize but I have just missed it somewhere.

Regards..,

MT

Recommended Answers

All 6 Replies

(wrong thread)......

(wrong thread)......

Thanks for replying but your example is similar to what I am trying to do but not exactly.

I am trying to resize an image keeping the aspect ratio but leaving it as 800px on the longest side.

The snippet I included on my original post is how it works out the resize if the width of the image is the longest side and then sets this as 800px and is supposed then to calculate the px for the height but like I said, it is rounding the digit down so in my earlier example:-

3000/800 = 3.75 but rounds to 3
then this 3 is used to calculate the height
2400/3 = 800

thus the outputted image becomes 800px x 800px,

instead of 800px x 640px

I really need the calculation not to round down at all so I can use 3.75 and not 3.

Regards..,

MT

After playing around some more I have figured it out.

if (origW > origH){
					double RorigW = (double)origW/800;
					double RorigH = (double)origH/RorigW;
					int ReH = Convert.ToInt32(RorigH);

I had to use the double statement and then convert the outcome back to an int.

All now working..,

MT

Console.WriteLine(Math.Round(((float)3000 / 800)));

You should study again data types, you can't force your math operations on int data type and what are real numbers is.

By the way check on Math.ceiling and Math.Floor methods it might help you further.

Console.WriteLine(Math.Round(((float)3000 / 800)));

You should study again data types, you can't force your math operations on int data type and what are real numbers is.

By the way check on Math.ceiling and Math.Floor methods it might help you further.

Thanks to you both, I may come back to this if I need it as the way I have done it now seems to work OK.

Kind regards..,

MT

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.