Question

I posted this other topic, got some gentle replies, but although the answers resolved the quality side of the question they created another issue with the image size in kb. Asp.net image resizing quality

Here is the issue.

I have a 2MB image that I want to reduce to 480px width.

I used three different ways to resize it:

1) On Fileupload ran this code:

System.IO.Stream st = FileUploadPost.PostedFile.InputStream;
myImage = System.Drawing.Image.FromStream(st);
thumb = myImage.GetThumbnailImage(newWidth, newHeight, null, System.IntPtr.Zero);
thumb.Save(myPath);

Benefits: Size in kb becomes small (around 80Kb).

Downside: Visual quality is horrible

2) On Fileupload ran this code (solution provided on the mentioned post):

 Bitmap newImg = new Bitmap(newWidth, newHeight,    System.Drawing.Imaging.PixelFormat.Format24bppRgb);                            
 Graphics newGraphic = Graphics.FromImage(newImg);
 newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
 newGraphic.Dispose();
 newImg.Save(myPath); 

Benefits: Visual quality is good

Downside: Size continues very big (around 400kb)

3) Used Windows Paint software and "manually" reduced the image to 480px width

Benefits: Benefits of both 1) and 2) -> Visual quality is good and size is reduced to around 80kb

Question is:

What is the code that reproduces item 3 behaviour (good visual quality and smaller size in kb)?

Thanks

Was it helpful?

Solution

Try one of these:

 Bitmap newImg = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);                            
 Graphics newGraphic = Graphics.FromImage(newImg);
 newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
 newGraphic.Dispose();
 newImg.Save(myPath); 

or

 Bitmap newImg = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format16bppArgb1555);                            
 Graphics newGraphic = Graphics.FromImage(newImg);
 newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
 newGraphic.Dispose();
 newImg.Save(myPath); 

or

 Bitmap newImg = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);                            
 Graphics newGraphic = Graphics.FromImage(newImg);
 newGraphic.DrawImage(myImage, 0, 0, newWidth, newHeight);
 newGraphic.Dispose();
 newImg.Save(myPath); 

This should only slightly degrade the quality and drastically reduce the size, Im not sure which one would be better though.

Here are a whole bunch of other options to try as well:

http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat%28v=vs.110%29.aspx

OTHER TIPS

   protected void Page_Load(object sender, EventArgs e)
    {
        //BR**
        string filePath = "";//Source file path with image name
        int CompressLevel = 50;//Image compression leve as per our requirement
        string DestintionPath = "";//Destination file path
        string Filename = "";//Output image name to save in destination path

        Stream bmpStream = System.IO.File.Open(filePath, System.IO.FileMode.Open);
        Bitmap bmp1 = new Bitmap(bmpStream);

        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, CompressLevel);
        myEncoderParameters.Param[0] = myEncoderParameter;
        bmp1.Save(DestintionPath + "\\" + Filename, jpgEncoder, myEncoderParameters);
        //BR**
        bmpStream.Close();
        string lblmsg = "Compressed Sucessfully with Compression Level { " + CompressLevel.ToString() + " }";
    }

    private ImageCodecInfo GetEncoder(ImageFormat format)
    {
        /*/ Hope this will work.Thanks in advance /*/
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top