asp.net 利用一般处理程序 怎么获取数据库中的图片(二进制数据)
一般处理程序
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using DingDan;
using WEIP.Common;
namespace DingDan.OA.Staff.Center
{
///
/// StffIcon 的摘要说明
///
public class StffIcon : IHttpHandler
{
//员工信息
ProductStaffBasic Psb = new ProductStaffBasic();
//员工信息实体
ProductStaffBasicEntity PsbE = new ProductStaffBasicEntity();
public void ProcessRequest(HttpContext context)
{
MemoryStream ms = null;
try
{
context.Response.ContentType = "text/plain";
HttpRequest request = context.Request;
int id = Convert.ToInt32(context.Request.QueryString["id"]);
PsbE = Psb.GetInfo(id);
Byte[] img = PsbE.Psblcon;
ms = new MemoryStream(img);
Image icon = Image.FromStream(ms);
int thumbnailWidth = TypeParse.StrToInt(request["tw"],-1);
int thumbnailHeight = TypeParse.StrToInt(request["th"],-1);
string mode = request.Params["mode"].ToString();
string module = request.QueryString["module"].ToString();
string tablename = module + "_ATTACHMENT";
WEIP.Sys.Attachment attach = new WEIP.Sys.Attachment("", tablename, true, "");
ms.Close();
//背景色
WEIP.Site.Thumbnail.BgColor color = WEIP.Site.Thumbnail.BgColor.White;
if (mode == "0")
{
color = WEIP.Site.Thumbnail.BgColor.White;
}
if (thumbnailWidth == -1 && thumbnailHeight == -1)
{
ImageFormat thisFormat = icon.RawFormat;
if (thisFormat.Equals(ImageFormat.Gif))
{
context.Response.ContentType = "image/gif";
}
else
{
context.Response.ContentType = "image/jpeg";
}
Bitmap outBmp = new Bitmap(icon);
outBmp.Save(context.Response.OutputStream, thisFormat);
}
else
new WEIP.Site.Thumbnail().GetSmallImage(context.Response, icon, thumbnailWidth, thumbnailHeight, color);
//写新图
}
catch
{
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
asp页面
/asp:HyperLink
数据加载时的方法
this.PImage.ImageUrl = "~/Staff/Center/StffIcon.ashx?id=" + Id + "&tw=150&th=150&mode=0&module=OUTSIDE_SITE";
this.LinkUrl.NavigateUrl = "~/Staff/Center/StffIcon.ashx?id=" + Id + "&mode=0&module=OUTSIDE_SITE";