0

I write a simple code to reduce image file size. This code can work with small file size, but when run with bigger file size get an error. This is the error,

An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll Additional information: Out of memory. enter image description here

All the image file size bigger than 800MB. And image type is JPEG and TIFF to convert to JPEG. This is my code

public void GetImageFile()
    {
        string path1 = string.Empty;
        string fileName = string.Empty;
        string fileFolder = ConfigurationManager.AppSettings["locationFrom"];
        string[] fileList = Directory.GetFiles(fileFolder, "*");

        foreach (string file in fileList)
        {
            fileName = System.IO.Path.GetFileName(file);

            path1 = "\\Photo\\newPath\\" + fileName;

            Bitmap bmp1 = new Bitmap(file);
            ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);

            System.Drawing.Imaging.Encoder myEncoder =
                System.Drawing.Imaging.Encoder.Quality;

            EncoderParameters myEncoderParameters = new EncoderParameters(1);

            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            bmp1.Save(path1, jpgEncoder, myEncoderParameters);

        }
    }

The error happen on this line,

Bitmap bmp1 = new Bitmap(file);

Is there any suggestion I can use to reduce/compress the image file size without reduce the image quality?

TheBrave SamWise
  • 85
  • 1
  • 2
  • 9
  • Does it run out of memory on the *first* image, or when processing multiple images? – dbc Jul 28 '15 at 04:07
  • @dbc yes..same for the first image and multiple image.. – TheBrave SamWise Jul 28 '15 at 05:59
  • Do you run out of memory loading a huge tiff file, or a huge jpeg? If tiff, see http://stackoverflow.com/questions/9004149/open-huge-tif-in-net-and-copy-parts-to-new-image. Also, are you running a 32 bit process or a 64 bit process? – dbc Jul 28 '15 at 06:16
  • @dbc it was tiff file and it's 32bit.. – TheBrave SamWise Jul 28 '15 at 06:19
  • 1) Can you switch to 64 bit? 2) Try [Open huge TIF in .NET and copy parts to new image](http://stackoverflow.com/questions/9004149/open-huge-tif-in-net-and-copy-parts-to-new-image) and see if it helps. 3) Try [How do I create a (32-bit) .NET application to use 3 GB RAM?](http://stackoverflow.com/questions/464458/how-do-i-create-a-32-bit-net-application-to-use-3-gb-ram). – dbc Jul 28 '15 at 06:22

0 Answers0