出自ProgWiki
用途
- Web-Server端,動態圖檔顯示。(透過GDI+做Jpeg轉檔)
-
- 自硬碟裡動態取出 QueryString["Filename"] 所指定的圖檔。
- 顯示出 Session["RegisterCode"] 的內容。
程式碼
<%@ WebHandler Language="C#" Class="ImageServices2" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
public class ImageServices2 : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest (HttpContext context)
{
Bitmap Bmp = null;
string strMode = context.Request.QueryString["Mode"];
switch( strMode )
{
case "Paste":
case "Temp":
{
System.Text.StringBuilder sbFile = new System.Text.StringBuilder(100);
sbFile.Append("C:\\").Append(context.Request.QueryString["Mode"]).Append("\\");
sbFile.Append(context.Request.QueryString["Filename"].Replace("-", "\\"));
//判斷檔案是否存在
if (File.Exists(sbFile.ToString()))
{
Bmp = new Bitmap(sbFile.ToString());
}
else
{
context.Response.Write("Not Exists");
context.Response.End();
}
}
break;
case "Register":
{
if (context.Session["RegisterCode"] != "")
{
//宣告 Bmp 圖形物件,建立動態字串圖示
Bmp = new Bitmap(240, 60, PixelFormat.Format16bppRgb565);
//定義繪圖元件、字形、筆刷
using (Graphics formGraphics = Graphics.FromImage(Bmp))
{
using (Font drawFont = new Font("Lucida Console", 32, FontStyle.Strikeout))
{
using (SolidBrush drawBrush = new SolidBrush(Color.Yellow))
{
StringFormat drawFormat = new StringFormat();
//繪製文字
string strDraw = context.Session["RegisterCode"] as string;
formGraphics.DrawString( strDraw,
drawFont,
drawBrush,
20.0f, 6.0f, drawFormat);
}
}
}
}
else
{
context.Response.Write("RegisterCode Is Nothing");
context.Response.End();
}
}
break;
default:
{
context.Response.Write("Else");
context.Response.End();
}
break;
}
//設定輸出類型
context.Response.ContentType = "image/Jpeg";
//處理圖形品質
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
//'找出 Encoder
foreach(ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
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, ImageFormat.Jpeg);
}
else
{
//儲存 - 高品質
Bmp.Save(context.Response.OutputStream, ici, ep);
}
//釋放
Bmp.Dispose();
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
使用範例
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="ImageServices.ashx?Mode=Register" alt=""/><br/>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["RegisterCode"] = "xyz";
}
}