Click here to Skip to main content
15,891,837 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi

i got an Out of memory exception when i used to retrive image from a folder

below code:

C#
public void Resize(string origPath, string savePath, int width, int height)
       {

           GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
           GC.WaitForPendingFinalizers();
           GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
           //RESIZES PICTURE TO MAX SIZE THAT IS WITHIN SUPPLIED RANGE
         


           //retrieve image
          Image SavedImage = Image.FromFile(origPath, false);//Exception coming
         //  Bitmap SavedImage = Image.FromFile(origPath) as Bitmap;


           //process based on resize dimensions
           if (SavedImage.Height > height && SavedImage.Width > width)
           {
               //both dimensions are bigger than pic

               //calc ratios
               decimal SavedRatio = (decimal)SavedImage.Width / (decimal)SavedImage.Height;
               decimal NewRatio = (decimal)width / (decimal)height;

               if (NewRatio > SavedRatio)
               {
                   //new dimensions are wider: resize by height
                   ResizeByHeight(SavedImage, savePath, height);
               }
               else
               {
                   //new dimensions are narrower: resize by width
                   ResizeByWidth(SavedImage, savePath, width);

               }
           }
           else if (SavedImage.Height < height && SavedImage.Width < width)
           {
               //both dimensions are less than pic

               //save - no changes necessary
               Save(SavedImage, savePath);
           }
           else if (SavedImage.Height > height && SavedImage.Width < width)
           {
               //height is bigger, width is smaller
               ResizeByHeight(SavedImage, savePath, height);

           }
           else if (SavedImage.Height < height && SavedImage.Width > width)
           {
               //width is bigger, height is smaller
               ResizeByWidth(SavedImage, savePath, width);

           }
           else if (SavedImage.Height == height && SavedImage.Width == width)
           {
               //dimensions are exactly equal to pic

               //save - no changes necessary
               Save(SavedImage, savePath);
           }


       }



please suggest any solution
Posted

Hi,

if you want to resize images I advice you to check following links:

http://weblogs.asp.net/gunnarpeipman/archive/2009/04/02/resizing-images-without-loss-of-quality.aspx[^]

Regards
Robert
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jan-12 3:40am    
Agree, a 5.
--SA
Kanasz Robert 28-Jan-12 3:52am    
thank you. couple days before I have used this solution and it works for me. :)
If the line
C#
Image SavedImage = Image.FromFile(origPath, false);//Exception coming
Is giving you an Out of Memory exception, then you need to look at the image file you are loading: what kind of resolution is it?
If it is bigger than approx 23,000 by 23,000 then you will probably get out of memory because no single item can be more than 2GB in .NET.

Otherwise, it should be ok, unless you have a real problem with the file content - have you tried with a different file?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900