ImageServices

出自 ProgWiki
前往: 導覽搜尋

用途

  • Web-Server端,圖檔縮放。(透過GDI+做圖檔縮放的計算)

程式碼

<%@ WebHandler Language="C#" Class="ImageServices" %>
 
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
 
public class ImageServices : IHttpHandler
{
    public void ProcessRequest (HttpContext context)
    {
        //TODO: void ProcessRequest (HttpContext context)
 
        //來源參數
        string strSrcPath   = context.Request["src"];
        //寬度參數
        string strWidth     = context.Request["width"];
        //高度參數
        string strHeight    = context.Request["height"];
        //輸出品質
        string strQuality   = context.Request["quality"];
        //是否使用比例刻度
        string strScale     = context.Request["scale"];
 
        //載入來源
        string strFile = context.Server.MapPath(strSrcPath);
        using (Bitmap bmpSrc = new Bitmap(strFile))
        {
            int nWidthSrc = bmpSrc.Width;
            int nHeightSrc = bmpSrc.Height;
 
            //參數解析(是否使用比例刻度)
            bool bIsScale;
            try
            {
                bIsScale = bool.Parse(strScale);
            }
            catch 
            {
                bIsScale = false;
            }
 
            //參數解析(寬度)
            int nWidth;
            try
            {
                if (bIsScale == true)
                {
                    nWidth = int.Parse(strWidth) * nWidthSrc / 100;
                }
                else
                {
                    nWidth = int.Parse(strWidth);
                }
            }
            catch
            {
                nWidth = nWidthSrc;
            }
 
            //參數解析(高度)
            int nHeight;
            try
            {
                if (bIsScale == true)
                {
                    nHeight = int.Parse(strHeight) * nHeightSrc / 100;
                }
                else
                {
                    nHeight = int.Parse(strHeight);
                }
            }
            catch
            {
                nHeight = nHeightSrc;
            }
 
            //參數解析(輸出品質)
            int nQuality; 
            try
            {
                nQuality = int.Parse(strQuality);
            }
            catch
            {
                nQuality = 100;
            }
 
            using (Bitmap bmpDest = new Bitmap(nWidth, nHeight, PixelFormat.Format24bppRgb))
            {
                using (Graphics grap = Graphics.FromImage(bmpDest))
                {
                    Rectangle rectSrc = new Rectangle(0, 0, nWidthSrc, nHeightSrc);
                    Rectangle rectDest = new Rectangle(0, 0, nWidth, nHeight);
 
                    grap.DrawImage(bmpSrc, rectDest, rectSrc, GraphicsUnit.Pixel);
 
                    //設定輸出類型
                    context.Response.ContentType = "image/Jpeg";
 
                    //處理圖形品質
                    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                    ImageCodecInfo ici = null;
 
                    //找出Encoder
                    int nLength = codecs.Length;
                    for (int i = 0; i < nLength; i++)
                    {
                        ImageCodecInfo codec = codecs[i];
                        if (codec.MimeType == "image/jpeg")
                        {
                            ici = codec;
                        }
                    }
 
                    //參數 - 高品質圖檔
                    EncoderParameters ep = new EncoderParameters();
                    ep.Param[0] = new EncoderParameter(Encoder.Quality, nQuality);
 
                    if ((ici == null) || (ep == null))
                    {
                        //儲存-低品質
                        bmpDest.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                    }
                    else
                    {
                        //儲存-高品質
                        bmpDest.Save(context.Response.OutputStream, ici, ep);
                    }
                    //結束
                    context.Response.End();
                }
            }
        }
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

使用範例

* 例如圖放在 http://127.0.0.1/images/test.jpg
* 這隻 ImageServices.ashx 放在 http://127.0.0.1/ImageServices/ImageServices.ashx

比例模式
* 長寬各70% http://127.0.0.1/ImageServices/ImageServices.ashx?src=/images/test.jpg&width=70&height=70&scale=true
* 長寬各60% http://127.0.0.1/ImageServices/ImageServices.ashx?src=/images/test.jpg&width=60&height=60&scale=true

像點模式
* 長寬各500pixel  http://127.0.0.1/ImageServices/ImageServices.ashx?src=/images/test.jpg&width=500&height=500&scale=false
* 長寬各1000pixel http://127.0.0.1/ImageServices/ImageServices.ashx?src=/images/test.jpg&width=1000&height=1000&scale=false