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
UPDATE
I fixed the error show in image by using following line of code
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);