Method to proportionately resize images does not resize correctly

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
Am I missing something? When I use my method the ratio of the old size to the new size is exactly the same but it is not proportional when it saves to disk.
The call and creation of the bitmap:
C#:
Bitmap b=new Bitmap(file);
	Size size= a.graphics.ProportionalSize(b.Size, new Size(Convert.ToInt32(this.widthTB.Text), Convert.ToInt32(this.hieghtTB.Text)));
			
					
Bitmap B=new Bitmap(b, size);
					
B.Save(thisDir+"\\x\\"+im, ImageFormat.Bmp);
The method that returns a proportional size:
C#:
public static Size ProportionalSize(Size imageSize, Size newSize)
{
	Size size=new Size(imageSize.Width, imageSize.Height);
	int ratio= size.Width-size.Height;
	if(size.Width < newSize.Width && size.Height < newSize.Height)
	{
MessageBox.Show(ratio+"");
					
		while(size.Width < newSize.Width && size.Height < newSize.Height)
		{
						size= new Size(size.Width+1, size.Height+1);
		}
ratio= size.Width-size.Height;
MessageBox.Show(ratio+"");
	}

	else
	{
ratio= size.Width-size.Height;
MessageBox.Show(ratio+"");
	                while(size.Width > newSize.Width || size.Height > newSize.Height)
		{
						size= new Size(size.Width-1, size.Height-1);
		}
				
MessageBox.Show(size+"  "+newSize);
	}
ratio= size.Width-size.Height;
MessageBox.Show(ratio+"");
	if(size.Width < 1) size=new Size(size.Width+-size.Width+1, size.Height-size.Width-1);
	if(size.Height < 1) size=new Size(size.Width-size.Height-1, size.Height+-size.Height+1);

	return size;
				
}
Lets say that I sent a size of 400, 295 to the method. Proportionately I get 400, 232 which is mathmatically correct. But it is not, when I look at the image proportional to the original. I used 400, 295 because that is what IrfanView says the proportions should be.

What am I missing? There must be something I dont know about resizing images.
 
Last edited by a moderator:
Does anyone have an idea what I do not know about resizing images. Mathmatically, my method works correctly but according to IrfanView you do not resize the images mathmatically correct but with some other calculation.

Please help if you know what I do not.
 
Im not sure what youre trying to do. You say:
Lets say that I sent a size of 400, 295 to the method. Proportionately I get 400, 232...

Im confused because your function takes two params and returns a new Size. You only mention passing one value and getting back a new value.

I havent looked at your code, but are you just trying to resize an image? Why not just multiply the width and height by the *same* value? If you need to base that scaling factor based on a maximum height or width (the second param maybe?) then you just need to figure out which dimension (width or height) is bigger and get your scaling value from that. If thats all you need, I can give you my 3 or 4 line version.

-Nerseus
 
Thank you Nerseus, that is exactly right. I changed my code around and it works perfectly now and is faster.
 
Back
Top