Unimatrix_001
asked on
Resizing images - aspect ratio not maintained
Hi,
The following code is contained within a function, the requested zoom factor is simply an integer that we add on to make the image larger. Once this function is ran I create a new image based off the original image with the newImageWidth and newImageHeight, although my problem is that when zooming in the aspect ratio (shown by Debug.WriteLine) does not remain constant, which it obviously should; what can I do to help this?
Thanks,
Uni
The following code is contained within a function, the requested zoom factor is simply an integer that we add on to make the image larger. Once this function is ran I create a new image based off the original image with the newImageWidth and newImageHeight, although my problem is that when zooming in the aspect ratio (shown by Debug.WriteLine) does not remain constant, which it obviously should; what can I do to help this?
Thanks,
Uni
///////////////////////////////////////////////////////////////////////////////////////////////////
//Prepare the variables.
///////////////////////////////////////////////////////////////////////////////////////////////////
int newImageWidth=0;
int newImageHeight=0;
///////////////////////////////////////////////////////////////////////////////////////////////////
//Calculate the aspect ratio of the frame and image from the width point of view and if the aspect
//ratio for the frame is higher then the picture will be height limited.
///////////////////////////////////////////////////////////////////////////////////////////////////
if(((float)Width/Height)>((float)img_OriginalImage.Width/img_OriginalImage.Height)){
newImageWidth=(int)(Height*((float)img_OriginalImage.Width/img_OriginalImage.Height))+aRequestedZoomFactor;
newImageHeight=Height+aRequestedZoomFactor;
Debug.WriteLine(((float)newImageWidth/newImageHeight).ToString());
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//Otherwise the aspect ratio of the frame must be lower than that of the aspect ratio of the
//picture. The aspect ratio we use for calculating the new width is from the point of the image
//height as opposed to the width.
///////////////////////////////////////////////////////////////////////////////////////////////////
else{
newImageWidth=Width+aRequestedZoomFactor;
newImageHeight=(int)(Width*((float)img_OriginalImage.Height/img_OriginalImage.Width))+aRequestedZoomFactor;
Debug.WriteLine(((float)newImageWidth/newImageHeight).ToString());
}
Try to work with floating point precision, only. And only when you need the integer value (e.g. for the newly created bitmap) convert it to int.
ASKER
I'm not quite sure how else I can do it other than with floats? Could you give an example?
Thanks,
Uni
Thanks,
Uni
You could try to use floats only to store the sizes:
Not:
int newImageWidth=0;
int newImageHeight=0;
but
float ...
everywhere you store image sizes. Give it a try.
Not:
int newImageWidth=0;
int newImageHeight=0;
but
float ...
everywhere you store image sizes. Give it a try.
ASKER
Call me daft, but I'm struggling to see how that would help? Take the following two lines:
newImageWidth=(int)(Height *((float)i mg_Origina lImage.Wid th/img_Ori ginalImage .Height))+ aRequested ZoomFactor ;
newImageHeight=Height+aReq uestedZoom Factor;
The only place where I could lose precision is (img_OriginalImage.Width/i mg_Origina lImage.Hei ght), but that result never changes irrelevant of the requested zoom factor, so I'm unsure at which points in the application the precision is being lost?
newImageWidth=(int)(Height
newImageHeight=Height+aReq
The only place where I could lose precision is (img_OriginalImage.Width/i
ASKER
...or my method of calculating the aspect ratio is not correct...
ASKER
Actually, I have found a couple of mistakes in my calculation:
///////////////////////////////////////////////////////////////////////////////////////////////////
//Calculate the aspect ratio of the frame and image from the width point of view and if the aspect
//ratio for the frame is higher then the picture will be height limited.
///////////////////////////////////////////////////////////////////////////////////////////////////
if(((float)Width/Height)>((float)img_OriginalImage.Width/img_OriginalImage.Height)){
newImageWidth=(int)(Height*((float)img_OriginalImage.Width/img_OriginalImage.Height))+aRequestedZoomFactor;
newImageHeight=Height+aRequestedZoomFactor;
//The above line I use Height*((float)... this should be img_OriginalImage.Height.
Debug.WriteLine(((float)newImageWidth/newImageHeight).ToString());
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//Otherwise the aspect ratio of the frame must be lower than that of the aspect ratio of the
//picture. The aspect ratio we use for calculating the new width is from the point of the image
//height as opposed to the width.
///////////////////////////////////////////////////////////////////////////////////////////////////
else{
newImageWidth=Width+aRequestedZoomFactor;
newImageHeight=(int)(Width*((float)img_OriginalImage.Height/img_OriginalImage.Width))+aRequestedZoomFactor;
//The above line I use Width*((float)... this should be img_OriginalImage.Width.
Debug.WriteLine(((float)newImageWidth/newImageHeight).ToString());
}
ASKER
Ack, sorry, corrections should be:
newImageHeight
newImageWidth
respectively.
newImageHeight
newImageWidth
respectively.
ASKER
Btw, will be AFK for a couple of hours or so...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi cmorbach,
Thank you! :) I'm back (obviously), so I'll take a look at your code.
Uni
Thank you! :) I'm back (obviously), so I'll take a look at your code.
Uni
ASKER
Hi there,
Thanks again for the code although I'm having great difficulty applying it to mine as I don't have anything like maxWidth or maxHeight?
Thanks again,
Uni
Thanks again for the code although I'm having great difficulty applying it to mine as I don't have anything like maxWidth or maxHeight?
Thanks again,
Uni
You shouldn't apply it but learn to understand it! ;)
As I don't have your code (in context) I can't fix anything but just give hints...can't you provide a ( standalone) sample project?
As I don't have your code (in context) I can't fix anything but just give hints...can't you provide a ( standalone) sample project?
ASKER
>>You shouldn't apply it but learn to understand it! ;)
I fully agree, although I've been having difficulty understanding what they are even there for. Anyway, I've done a bit of rewriting and it seems that I've got it working, I'll accept one of your comments as after all you did provide a solution. :)
Thanks,
Uni
I fully agree, although I've been having difficulty understanding what they are even there for. Anyway, I've done a bit of rewriting and it seems that I've got it working, I'll accept one of your comments as after all you did provide a solution. :)
Thanks,
Uni
ASKER
Thank you. :)