RoundedImage.ashx

出自 ProgWiki
前往: 導覽搜尋

用途

  • Web-Server端,圓角圖檔產生。(透過GDI+畫出各90度的圓角圖)

程式碼

<%@ WebHandler Language="C#" Class="RoundedImage" %>
 
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
 
public class RoundedImage : IHttpHandler
{
	public void ProcessRequest (HttpContext context) 
	{
		try
		{
			//圖的長寬
			int nSize = int.Parse( context.Request.QueryString["size"]);
			//線的粗細
			int nLine = int.Parse(context.Request.QueryString["line"]);
			//線的顏色
			string strColor = context.Request.QueryString["color"];
			//底色
			string strBkColor = context.Request.QueryString["bkcolor"];
			//圓角的方向
			string strMode = context.Request.QueryString["mode"];
			//輸出的圖檔格式
			string strType = context.Request.QueryString["type"];
 
			using (Bitmap Bmp = new Bitmap(nSize, nSize, PixelFormat.Format32bppArgb))
			{
				using (Graphics formGraphics = Graphics.FromImage(Bmp))
				{
					if (!string.IsNullOrEmpty(strBkColor))
						formGraphics.Clear(Color.FromName(strBkColor));
 
					using (Pen objPen = new Pen(Color.FromName(strColor), nLine))
					{
						// Create location and size of ellipse.
						int x, y, width, height;
 
						// Create start and sweep angles.
						int startAngle, sweepAngle;
 
						switch (strMode.ToLower())
						{			
							//左上
							case "tl":
								x = 0 + (nLine / 2);
								y = 0 + (nLine / 2);
								width = nSize * 2;
								height = nSize * 2;
								startAngle = 180;
								sweepAngle = 90;
								break;
							//右上
							case "tr":
								x = -nSize - (nLine / 2);
								y = 0 + (nLine / 2);
								width = nSize*2;
								height = nSize * 2;
								startAngle = 270;
								sweepAngle = 90;
								break;
							//右下
							case "br":
								x = 0 + (nLine / 2);
								y = -nSize - (nLine / 2);
								width = nSize * 2;
								height = nSize * 2;
								startAngle = 90;
								sweepAngle = 90;
								break;
							//右下
							case "bl":
								x = -nSize - (nLine / 2);
								y = -nSize - (nLine / 2);
								width = nSize * 2;
								height = nSize * 2;
								startAngle = 0;
								sweepAngle = 90;
								break;	
							//圓	
							default:
								x = 0 + (nLine / 2);
								y = 0 + (nLine / 2);
								width = nSize;
								height = nSize;
								startAngle = 0;
								sweepAngle = 360;
								break;
						}
 
						// Draw Arc
						formGraphics.DrawArc(objPen, x, y, width, height, startAngle, sweepAngle);
					}
				}
 
				string strContentType;
				string strMimeType;
				ImageFormat ImageFileFormat;
 
				switch (strType.ToLower())
				{
					case "gif":
						strContentType = "image/Gif";
						strMimeType = "image/gif";
						ImageFileFormat = ImageFormat.Gif;
						break;
 
					case "jpeg":
					default:
						strContentType = "image/Jpeg";
						strMimeType = "image/jpeg";
						ImageFileFormat = ImageFormat.Jpeg;
						break;
				}
 
				//設定輸出類型
				context.Response.ContentType = strContentType;
 
				//處理圖形品質
				ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
				ImageCodecInfo ici = null;
 
				//'找出 Encoder
				foreach (ImageCodecInfo codec in codecs)
				{
					if (codec.MimeType == strMimeType)
						ici = codec;
				}
 
				//參數 - 高品質圖檔
				EncoderParameters ep = new EncoderParameters();
				int nQuality = 100;
				ep.Param[0] = new EncoderParameter(Encoder.Quality, nQuality);
 
				if ((ici == null) || (ep == null))
				{
					//儲存 - 低品質
					Bmp.Save(context.Response.OutputStream, ImageFileFormat);
				}
				else
				{
					//儲存 - 高品質
					Bmp.Save(context.Response.OutputStream, ici, ep);
				}
			}
			context.Response.End();
		}
		catch (Exception ex)
		{
			context.Response.ContentType = "text/plain";
			context.Response.Write(ex.ToString());
		}
	}
 
	public bool IsReusable
	{
        	get
		{
            		return false;
        	}
	}
}

使用範例

* 左上
http://localhost/RoundedImage.ashx?size=64&line=10&mode=tl&color=red&bkcolor=white&type=jpeg
* 右下
http://localhost/RoundedImage.ashx?size=64&line=10&mode=tr&color=red&bkcolor=white&type=jpeg
* 左下
http://localhost/RoundedImage.ashx?size=64&line=10&mode=bl&color=red&bkcolor=white&type=jpeg
* 右下
http://localhost/RoundedImage.ashx?size=64&line=10&mode=br&color=red&bkcolor=white&type=jpeg