Answered by:
strange out memory error

Question
-
User1596203943 posted
I have a script which allows the user to upload an image file, alongside the original image I wish to create a thumbnail version of the original image where neither the width nor height would exceed 50 pixels and the ratio is maintained. I run this if statement to determine the new size I require. if (intOldWidth >= intOldHeight){
iW50 =
iH50 = Convert.ToInt32((50 / intOldWidth) * intOldHeight); } else{
iW350 = Convert.ToInt32((50 / intOldHeight) * intOldWidth);
iH50 = 50;}
I then call this command to create the thumbnail image:
System.Drawing.Image img50 = imgInput.GetThumbnailImage(iW50, iH50, null, IntPtr.Zero);
My problem is that if i use my if statement to calculate the relative sizes i get the out of memory error - however if i hardcode the values in it works fine. Can anyone offer any guidance on this?
Thanks.Tuesday, October 7, 2008 9:26 AM
Answers
-
User187056398 posted
What are the values being passed to GetThumbNailImage?
protected void Button1_Click(object sender, EventArgs e) { int intOldHeight = 500; int intOldWidth = 500; // Test1 will equal zero int Test1 = Convert.ToInt32((50 / intOldHeight) * intOldWidth); // Test2 will equal 50 int Test2 = Convert.ToInt32((50.0 / intOldHeight) * intOldWidth); // another way int Test3 = (int)((50.0 / intOldHeight) * intOldWidth); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 7, 2008 11:25 AM
All replies
-
User187056398 posted
In integer math, 1 / 2 is zero.
Try using 50.0 to force floating point math.
Tuesday, October 7, 2008 9:57 AM -
User1596203943 posted
I'm sorry, i don' think I kow exactly what you are suggesting.
I tried doing the ratio calculation as a float and then converting this to an int when setting the size - but i still got the same Out of memory error displayed.Tuesday, October 7, 2008 11:11 AM -
User187056398 posted
What are the values being passed to GetThumbNailImage?
protected void Button1_Click(object sender, EventArgs e) { int intOldHeight = 500; int intOldWidth = 500; // Test1 will equal zero int Test1 = Convert.ToInt32((50 / intOldHeight) * intOldWidth); // Test2 will equal 50 int Test2 = Convert.ToInt32((50.0 / intOldHeight) * intOldWidth); // another way int Test3 = (int)((50.0 / intOldHeight) * intOldWidth); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 7, 2008 11:25 AM -
User1596203943 posted
excellent, i used the (int)((50.0 / intOldHeight) * intOldWidth); approach and it works perfectly.
Many thanks.
Tuesday, October 7, 2008 11:34 AM