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.
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?