I am trying to change resolution of my jpg image using c#. I have refer this code that written in this link How to change resolution (DPI) of an image? its work fine but I have getting another issue for grayscale images. if I will try to save grayscale image using this code then it change the bit depth 8 to 24.
So I have try below code but I am getting error like "Bitmap region is already locked". I don't know how to pass guid in GetEncoderParameterList().
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
//Code
using (Image bitmap = Image.FromFile(pagePath))
{
using (Bitmap newBitmap = new Bitmap(bitmap))
{
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
EncoderParameters para = newBitmap.GetEncoderParameterList(jpgEncoder.Clsid);
imageResolution = (int)newBitmap.HorizontalResolution;
newBitmap.SetResolution(250, 250);
newBitmap.Save("file300.jpg", jpgEncoder, para);
}
}
Exception :