我有一个显示图像的图像处理程序。如果数据库没有针对其用户ID的记录,则一次显示默认图像。这是我的处理程序代码:
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Text;
using System.Data;
using System.Linq;
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
if (!string.IsNullOrEmpty(context.Request.QueryString["id"]))
{
int id = int.Parse(context.Request.QueryString["id"]);
Image image = null;
if (GetImage(id).PropertyItems.Length > 0)
{
image = GetImage(id);
context.Response.ContentType = "image/jpeg";
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
else
{
image.Save(HttpContext.Current.Server.MapPath("~/img/profile_user.jpg").ToString());
}
}
else
{
context.Response.ContentType = "text/html";
context.Response.Write("<p>Need a valid id</p>");
}
}
public bool IsReusable
{
get
{
return false;
}
}
private Image GetImage(int id)
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
int getProfileID = db.ProfilePhotos.Where(p => p.UserID.Equals(id)).Select(p => p.ProfileID).FirstOrDefault();
MemoryStream stream = null;
if (getProfileID > 0)
{
byte[] imgData = db.ProfilePhotos.Where(p => p.UserID.Equals(id)).Select(p => p.ProfileImage).FirstOrDefault().ToArray();
stream = new MemoryStream(imgData);
}
return Image.FromStream(stream);
}
}
}
这是我从此处理程序获取图像的代码:
imgUser.ImageUrl = "~/ImageHandler.ashx?id=" + CommonLogic.GetSessionValue("user_id").ToString();
当user_id存在而又没有像这样的图像显示时,这怎么工作呢?
这是怎么回事。请帮助我...