|
-
May 25th, 2004, 06:34 AM
#1
Thread Starter
Frenzied Member
Aspect Ratio
I need to resize an image(shrink) and maintain the aspect ratio. I have a constant new width 500px and of course I know how big it is to start with.
This is what I have. It works about 90% of the time. In fact it migrated 1000+ images. Total mb for images to start was 150mb after it is 36mb, and all the images look very good.
Code:
int nHeight = ((int)( 100 * (int)System.Math.Round(((decimal)500)/((decimal)bmp.Width)) * bmp.Height) / 100);
I know it's bad to do all that on one line. So far it blows up on images that are 1024x768 with a new height = 0. I don't know what the problem is other than the values being calculated when it craps out are like 0.4567 and the it just goes bad. It took me a week to find this way that almost works all the time... somebody please help.
Magiaus
If I helped give me some points.
-
May 25th, 2004, 06:36 AM
#2
Thread Starter
Frenzied Member
entire method as it is now
Code:
public static System.Drawing.Bitmap WebOptimize(System.Drawing.Bitmap bmp)
{
if(bmp.Width > 500)
{ //((int)( 100 * (int)System.Math.Round(((decimal)500)/((decimal)bmp.Width)) * bmp.Height) / 100);
double iWidth = 0; //500 / bmp.Width;
double iRatioPercent = 0; //100 * iWidth
double iHeight = 0; //iRatioPercent * bmp.Height
int nHeight = 0; //iHeight / 100
iWidth = 500 / bmp.Width;
//if(iWidth <= 0){iWidth = 1;}
iRatioPercent = 100 * iWidth;
//if(iRatioPercent <= 0){iRatioPercent = 1;}
iHeight = iRatioPercent * bmp.Height;
//if(iHeight < 0){iHeight = System.Math.Round(iHeight);}
iHeight = iHeight / 100;
nHeight =(int)System.Math.Round(iHeight);
if(nHeight > 0)
{
System.Drawing.Bitmap nBmp = new System.Drawing.Bitmap(500, nHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(nBmp);
g.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, 500, nHeight), 0, 0, bmp.Width, bmp.Height, System.Drawing.GraphicsUnit.Pixel);
return nBmp;
}
}
return bmp;
}
this is C# code btw
Magiaus
If I helped give me some points.
-
May 25th, 2004, 07:12 AM
#3
Is this C++?
I am not sure but I guess that since you are using int in the calculations you are getting numbers that are under 0.5, and those are rounded to 0. If you try to use double, then cast that to int after the calculation, then you might get a better answer. But not sure. Havn't looked that closely on your code
-
May 25th, 2004, 07:25 AM
#4
And BTW you should really try to devide that line, so it is easier too see...I have used a minute too look at it....and for me it looks like you are mulitpluing and dividing the same number on 100....try to split it up, it is easier to see what goes wrong that way that way...
-
May 25th, 2004, 07:33 AM
#5
Ex-Super Mod'rater
Re: entire method as it is now
Posted by NoteMe
Is this C++?
Posted by Magiaus
this is C# code btw
When your thread has been resolved please edit the original post in the thread (  )
and amend "-[RESOLVED]-" to the end of the title and change the icon to  , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

-
May 25th, 2004, 07:42 AM
#6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|