I'm trying to using a Generic Handler to retrieve and display images that are stored in a database.
But its just not working. Ive tried verious of the code below, but I cant seem to get it to work.
Can anyonne spot what I am doing wrong, or have some suggestions?
<%@ 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
开发者_Go百科 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;
}
}
}
wow, ok. Not sure how much of that is old code, what should be commented out or w/e but try something like this:
public void ProcessRequest (HttpContext context) {
int iconId;
if (string.IsNullOrEmpty(context.Request.QueryString["id"]) || !int.TryParse(context.Request.QueryString["id"], out iconId) )
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/gif";
var db = new UdINaturen.UdINaturenContext();
var GetIcon = (from i in db.subcategoryicons
where i.id == iconId
select i.picture).FirstOrDefault();
byte[] buffer = (byte[])Convert.FromBase64String(GetIcon);
context.Response.ContentType = "image/gif";
context.Response.BinaryWrite(buffer);
context.Response.Flush();
}
精彩评论