我正在尝试通过处理程序在网格视图中显示图像。但是当数据库中没有路径时,它会显示脏的无图像徽标。我需要在没有图像的地方显示“NOImage”图像。
<asp:TemplateField HeaderText="Path_Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "LargeImage.ashx?p=" + Eval("Name") + "&q=" + Eval("Vertual_Path") %>' />
</ItemTemplate>
</asp:TemplateField>
///大图像.ashx..
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
public class LargeImage : IHttpHandler
{
private Regex _nameValidationExpression = new Regex(@"[^\w/]");
private int _thumbnailSize = 250;
public void ProcessRequest(HttpContext context)
{
string photoName = context.Request.QueryString["p"];
string P_Path = context.Request.QueryString["Q"];
if (photoName == "" )
{
//file = (byte[])reader["Image"];
// FileStream fs = File.OpenRead(HttpContext.Current.Server.MapPath("~/Article/NoImage.png"));
//File = new byte[fs.Length];
//fs.Read(File, 0, File.Length);
context.Server.MapPath("~/Article/NoImage.jpg");
}
else
{
if (_nameValidationExpression.IsMatch(photoName))
{
throw new HttpException(404, "Invalid photo name.");
}
string cachePath = Path.Combine(HttpRuntime.CodegenDir, photoName + ".Large.png");
if (File.Exists(cachePath))
{
OutputCacheResponse(context, File.GetLastWriteTime(cachePath));
context.Response.WriteFile(cachePath);
return;
}
else
{
}
//string photoPath = context.Server.MapPath("~/Photo/" + photoName + ".jpg");
// string photoPath = context.Server.MapPath("~/Photo/" + photoName + ".jpg");
string photoPath = context.Server.MapPath(P_Path + "\\" + photoName + ".jpg");
Bitmap photo;
try
{
photo = new Bitmap(photoPath);
}
catch (ArgumentException)
{
throw new HttpException(404, "Photo not found.");
}
context.Response.ContentType = "image/pjpeg";
int width, height;
if (photo.Width > photo.Height)
{
width = _thumbnailSize;
height = photo.Height * _thumbnailSize / photo.Width;
}
else
{
width = photo.Width * _thumbnailSize / photo.Height;
height = _thumbnailSize;
}
Bitmap target = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(target))
{
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.DrawImage(photo, 0, 0, width, height);
using (MemoryStream memoryStream = new MemoryStream())
{
//context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + photoName + "\"");
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + DateTime.UtcNow.ToString() + "\"");
target.Save(memoryStream, ImageFormat.Jpeg);
// OutputCacheResponse(context, File.GetLastWriteTime(photoPath));
// OutputCacheResponse(context, File.GetLastWriteTime(photoPath));
using (FileStream diskCacheStream = new FileStream(cachePath, FileMode.CreateNew))
{
memoryStream.WriteTo(diskCacheStream);
}
memoryStream.WriteTo(context.Response.OutputStream);
context.Response.Flush();
}
}
}
}