在asp.net中使用Handler调整动态图像大小

时间:2013-09-11 10:19:49

标签: c# asp.net image-resizing

我想使用asp.net处理程序动态地重新调整图像大小。

我将此文章用作source

源代码

<%@ 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();
                    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;
        }
    }

}

.ASPX

<asp:Image ID="img1" runat="server" ImageUrl="~/Handler.ashx?h=50&w=50&file=Winter.jpg" />

这种方法很好但结果很模糊,还有改进的余地,这样可以提高图像质量。

0 个答案:

没有答案