2010-05-25 52 views
1

如何顯示在C#中使用WebBrowser控件編碼的圖像base64?如何將數據uri方案顯示到C#WebBrowser控制器

我用下面的代碼:

<img src="data:image/gif;base64,/9j/4AAQSkZJRgABAgAAZABkAA7AAR 
R894ADkFkb2JlAGTAAAAAAfbAIQABAMDAwMDBAMDBAYEAwQGBwUEBAUHCAYGBw 
... 
uhWkvoJfQO2z/rf4VpL6CX0Dts/63+FaS+gl9A7bP+tthWkvoJfQODCde4qfcg 
RiNWK3UyUeX9CXpHU43diOK915X5fG/reux5hUAUBftZ" /> 

但不顯示圖像。一種解決方案是在本地保存圖像並使用絕對路徑,但這不是可取的。

有什麼想法?

回答

0

我試過這樣做的項目和IE(WebBrowser控制將最終使用)成爲限制因素 - 它只能容納32Kb大小的圖像。我最後不得不創建一個基於數據庫密鑰返回圖像的HTTP處理程序(.ashx)。

編輯:示例 - 請注意數據庫處理例程是專有的,您必須自行放置。處理程序的其餘部分將展示如何重新調整圖像(如果需要),併發送回作爲瀏覽器的響應:

public class GenerateImage : IHttpHandler 
{ 
    /// <summary> 
    /// Shortcut to the database controller. Instantiated immediately 
    /// since the ProcessRequest method uses it. 
    /// </summary> 
    private static readonly IDataModelDatabaseController controller = 
     DataModelDatabaseControllerFactory.Controller; 

    /// <summary> 
    /// Enables processing of HTTP Web requests by a custom HttpHandler 
    /// that implements the <see cref="T:System.Web.IHttpHandler"/> 
    /// interface. 
    /// </summary> 
    /// <param name="context">An <see cref="T:System.Web.HttpContext"/> 
    /// object that provides references to the intrinsic server objects 
    /// (for example, Request, Response, Session, and Server) used to 
    /// service HTTP requests.</param> 
    public void ProcessRequest(HttpContext context) 
    { 
     if (controller == null) 
     { 
      return; 
     } 

     IDataModelDescriptor desc = controller.GetDataModelDescriptor(
      new Guid(context.Request.QueryString["dataModel"])); 
     IDataModelField imageField = 
      desc.Fields[context.Request.QueryString["imageField"]]; 
     IDatabaseSelectQuery query = controller.CreateQuery(); 
     string[] keys = context.Request.QueryString["key"].Split(','); 
     string showThumb = context.Request.QueryString["showThumbnail"]; 
     bool showThumbnail = showThumb != null; 

     query.AssignBaseTable(desc); 
     query.AddColumn(imageField, false); 
     for (int i = 0; i < desc.KeyFields.Count; i++) 
     { 
      query.AddCompareValue(
       desc.KeyFields[i], 
       keys[i], 
       DatabaseOperator.Equal); 
     } 

     context.Response.CacheControl = "no-cache"; 
     context.Response.ContentType = "image/jpeg"; 
     context.Response.Expires = -1; 

     byte[] originalImage = (byte[])controller.ExecuteScalar(query); 

     if (showThumbnail) 
     { 
      int scalePixels; 

      if (!int.TryParse(showThumb, out scalePixels)) 
      { 
       scalePixels = 100; 
      } 

      using (Stream stream = new MemoryStream(originalImage)) 
      using (Image img = Image.FromStream(stream)) 
      { 
       double multiplier; 

       if ((img.Width <= scalePixels) 
        && (img.Height <= scalePixels)) 
       { 
        context.Response.BinaryWrite(originalImage); 
        return; 
       } 
       else if (img.Height < img.Width) 
       { 
        multiplier = (double)img.Width/(double)scalePixels; 
       } 
       else 
       { 
        multiplier = (double)img.Height/(double)scalePixels; 
       } 

       using (Bitmap finalImg = new Bitmap(
        img, 
        (int)(img.Width/multiplier), 
        (int)(img.Height/multiplier))) 
       using (Graphics g = Graphics.FromImage(finalImg)) 
       { 
        g.InterpolationMode = 
         InterpolationMode.HighQualityBicubic; 
        finalImg.Save(
         context.Response.OutputStream, 
         ImageFormat.Jpeg); 
       } 
      } 
     } 
     else 
     { 
      context.Response.BinaryWrite(originalImage); 
     } 
    } 

    /// <summary> 
    /// Gets a value indicating whether another request can use the 
    /// <see cref="T:System.Web.IHttpHandler"/> instance. 
    /// </summary> 
    /// <value></value> 
    /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> 
    /// instance is reusable; otherwise, false. 
    /// </returns> 
    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 
+0

問題是我的應用程序沒有在asp.net writen,我用web瀏覽器的Controler從C#。我怎樣才能讓ProcessRequest函數執行? – Emanuel 2010-05-25 21:27:41

+0

我認爲你將不得不選擇將圖像放在本地路徑上。除非您可以集中託管它們,並通過網絡通過UNC路徑或通過http訪問的Web服務器訪問它們。 – 2010-05-25 22:01:57

0

什麼是數據uri字符串長度,根據data Protocol in IE8數據URI不能大於32,768個字符。

編輯:資源數據必須正確編碼;否則,會發生錯誤並且資源未加載。 「#」和「%」字符必須進行編碼,以及控制字符,非美國ASCII字符和多字節字符。

相關問題