0

I upload large image let us say 600x400 and i down size it to 200x133 using an image handler.

But the quality of the image not good after procssing it with image handler.

I use following code for ImageHandler.ashx

BELOW IS THE CODE FOR IMAGE-HANDLER

<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

public class ImageHandler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        // for new height of image
        int h = int.Parse(context.Request.QueryString["h"].ToString());
        // for new width of image
        int w = int.Parse(context.Request.QueryString["w"].ToString());
        // for  image file name
        string file = context.Request.QueryString["file"].ToString();

        // Path of image folder where images files are placed
        string filePath = context.Server.MapPath("~/images/" + file);

        // Resize proccess
        using (System.Drawing.Image img = System.Drawing.Image.FromFile(filePath))
        {
            Bitmap objBmp = new Bitmap(img, w, h);
            string extension = Path.GetExtension(filePath);
            MemoryStream ms;
            byte[] bmpBytes;
            switch (extension.ToLower())
            {
                case ".jpg":
                case ".jpeg":
                    ms = new MemoryStream();
                    //System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                    //EncoderParameters myEncoderParameters = new EncoderParameters(1);
                    //myEncoderParameter = new EncoderParameter(myEncoder, 100L);
                    //myEncoderParameters.Param[0] = myEncoderParameter;
                    objBmp.Save(ms, ImageFormat.Jpeg);
                    bmpBytes = ms.GetBuffer();
                    context.Response.ContentType = "image/jpeg";
                    context.Response.BinaryWrite(bmpBytes);
                    objBmp.Dispose();
                    ms.Close();
                    context.Response.End();

                    break;
                case ".png":
                    ms = new MemoryStream();
                    objBmp.Save(ms, ImageFormat.Png);
                    bmpBytes = ms.GetBuffer();
                    context.Response.ContentType = "image/png";
                    context.Response.BinaryWrite(bmpBytes);
                    objBmp.Dispose();
                    ms.Close();
                    context.Response.End();
                    break;
                case ".gif":
                    ms = new MemoryStream();
                    objBmp.Save(ms, ImageFormat.Gif);
                    bmpBytes = ms.GetBuffer();
                    context.Response.ContentType = "image/png";
                    context.Response.BinaryWrite(bmpBytes);
                    objBmp.Dispose();
                    ms.Close();
                    context.Response.End();
                    break;

            }
            img.Dispose();
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

With this code i am not able to get a good quality image. and when un-comment following code

                //System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                //EncoderParameters myEncoderParameters = new EncoderParameters(1);
                //myEncoderParameter = new EncoderParameter(myEncoder, 100L);
                //myEncoderParameters.Param[0] = myEncoderParameter;

It then generates following error.

I bind image to image control using following code.

  imgNews.ImageUrl = "../ImageHandler.ashx?h=200&w=133&file=../images/" + ImageName;

Please advice how i can improve quality of the image. with some image this code may work fine but most of the downsized images are not that good quality.

If i dont use image handler and just specify the width for image then image looks very good in FF, Chrome, Safar but not in IE 8,9 and 10 as the algorithm used by IE to downsize image in not good (this is what i red a lot on internet) I hope they have fixed this issue on IE11

enter image description here

UPDATE

I fixed the error show in image by using following line of code

EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
Learning
  • 19,469
  • 39
  • 180
  • 373
  • I hear good things about http://imageresizing.net/. And maybe a css of `img { -ms-interpolation-mode: bicubic; }` could just fix IE anyway. – Malk Mar 05 '14 at 04:26
  • I have tried css tricks in past but it doen't make much of difference ... May be i was doing something wrong – Learning Mar 05 '14 at 04:32
  • Check out the code in this answer http://stackoverflow.com/a/1922086/507793, it sets the interpolation mode of the transformation, which results in better quality when resizing. – Matthew Mar 05 '14 at 05:03

0 Answers0