asp.net
  • handler
  • 2012-09-13 43 views 0 likes 
    0

    我想通過處理程序在網格視圖中顯示圖像。但是當數據庫中沒有路徑時,它會顯示髒圖像標誌。我需要在沒有圖像的地方顯示「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> 
    

    ///LargeImage.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(); 
           } 
          } 
         } 
        } 
    

    回答

    0

    您可以使用此代碼嘗試 - 基於的ProcessRequest

    public class ImageHandler : IHttpHandler 
    { 
    public void ProcessRequest(System.Web.HttpContext ctx) 
    { 
         if(condition) // Adjust your condition 
         {  
         var path = ctx.Server.MapPath("~/images/NoImage.gif"); 
         var contentType = "image/gif"; 
         ctx.Response.ContentType = contentType; 
         ctx.Response.WriteFile (path); 
         } 
    
    } 
    
    public bool IsReusable { get {return true; } }   
    
    
    } 
    
    +0

    謝謝!!!救了我 ! –

    +0

    我很樂意幫助你Saaza,謝謝 –

    0

    在頁面LargeImage.ashx,如果沒有記錄/圖像渲染,做一個重定向到你本地的靜態圖像(在你的圖片文件夾中說)。

    這樣你就不依賴瀏覽器特定的「破碎」圖像。

    Response.ContentType = "image/jpeg" 
    Try 
        photo = GetPhoto(param) 
         IF photo is Nothing then 
          Response.Redirect("images/unknown.jpg", True) 
         Else 
          Response.Binarywrite(photo) 
         End if 
        Catch 
          Response.Redirect("images/unknown.gif", True) 
        Finally 
         photo.Dispose() 
        End Try 
    
    0

    在閱讀你的代碼,它看起來你的問題是HttpException(404, 「無效的圖片名稱。」 )你在投擲。

    瀏覽器會將其解釋爲破碎的圖像。

    用重定向替換那些已知的本地圖像。

    相關問題