Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

please help me with image.GetThumbnailImage (it create very low quality image)

i use this code to create thumbnails

System.Drawing.Image.GetThumbnailImageAbort abort = new System.Drawing.Image.GetThumbnailImageAbort(this.ThumbnailCallback);
System.Drawing.Image image2 = image.GetThumbnailImage((int)Math.Round((double)wid / difference), (int)Math.Round((double)hei / difference), abort, IntPtr.Zero);
image2.Save(str2, System.Drawing.Imaging.ImageFormat.Jpeg);
image2.Dispose();

but i get this very low quality image

alt text

but it is suposed to be like this one

alt text

what i am making wrong or how can achieve this

like image 354
Mariam Avatar asked Oct 17 '25 01:10

Mariam


1 Answers

Your problem is not really with the GetThumbnailImage() method, but instead in how you are saving the file. You need to specify the quality level of the JPEG you are saving, or it seems it always defaults to a very low value.

Consider this code as a guide (it's from an old .NET 2.0 project; the code still works fine compiled against 4.0, but there may be a more direct method in 4.0; I've never had reason to check)

ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegEncoder = null;
for (int x = 0; x < encoders.Length; x++) {
    if (string.Compare(encoders[x].MimeType, "image/jpeg", true) == 0) {
        jpegEncoder = encoders[x];
        break;
    }
}
if (jpegEncoder == null) throw new ApplicationException("Could not find JPEG encoder!");
EncoderParameters prms = new EncoderParameters(1);
prms.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80L);
bitmap.Save(fileName, jpegEncoder, prms);
like image 87
Andrew Barber Avatar answered Oct 18 '25 16:10

Andrew Barber