Results 1 to 6 of 6

Thread: Aspect Ratio

  1. #1

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    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.

  2. #2

    Thread Starter
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    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.

  3. #3
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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...

  5. #5
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    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.

  6. #6
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Hehehe...ok...my first line of C# is comming up...this should work...I hope...:

    Code:
    int newHieght = (int)((500.0 / (double)bmp.Width) * (double)bmp.Height);


    Edited...if there is nothing called doufle in C# then use float, or decimal..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width