I have made a method to CompressImageSize according to Image quality. The code for it is
public static Image CompressImage(string imagePath, long quality)
{
Image srcImg = LoadImage(imagePath);
//Image srcImg = Image.FromFile(imagePath);
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
ImageCodecInfo encoder = GetCodecInfo("image/jpeg");
srcImg.Save("d:\\creatives\\abcd123.jpg", encoder, parameters);
}
public static Image LoadImage(string filename)
{
using (FileStream fs = new FileStream(filename, FileMode.Open))
{
return(Image.FromStream(fs));
}
}
Now, when i run this code as is it gives me a 'Generic GDI+ exception' while saving the srcImg(last line in func #1), BUT when i uncomment the 2nd line and load the image using Image.FromFile everything works fine.
Why ??