我试图使用通用处理程序检索并显示存储在数据库中的图像。动态图像(从数据库)使用通用处理程序
但它只是不工作。我试过了下面的代码,但我似乎无法让它工作。
can anyonne spot我做错了,还是有一些建议?
<%@ WebHandler Language="C#" Class="IconsDb" %>
using System;
using System.Web;
using System.Linq;
using System.Data.Entity;
public class IconsDb : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
Int32 iconId;
if (context.Request.QueryString["id"] != null)
iconId = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/gif";
//System.IO.Stream strm = ShowEmpImage(iconId);
var db = new UdINaturen.UdINaturenContext();
var GetIcon = (from i in db.subcategoryicons
where i.id == iconId
select i.picture).FirstOrDefault();
object img = GetIcon;
System.IO.MemoryStream memStream= new System.IO.MemoryStream((byte[])Convert.FromBase64String(GetIcon));
System.Drawing.Bitmap bitImage=new System.Drawing.Bitmap((System.Drawing.Bitmap)System.Drawing.Image.FromStream(memStream));
byte[] buffer = memStream.ToArray();
context.Response.ContentType = "image/gif";
//context.Response.OutputStream.Write(buffer, 0, buffer.Length);
//context.Response.WriteFile();
context.Response.BinaryWrite(buffer);
//context.Response.Flush();
}
public bool IsReusable {
get {
return true;
}
}
}
你为什么要在第2行内容类型设置为纯文本? – kd7 2010-02-09 18:01:28
你为什么要将字节数组转换成一个MemoryStream来将一个Bitmap转换成一个字节数组? – dtb 2010-02-09 18:03:11