2016-04-28 36 views
0

我從數據庫中獲取圖像字節,並希望以字符串格式在圖像src中渲染thias。如何在運行時渲染該圖像? 這是我的場景。如何使用MVC c#在運行時在字符串中顯示圖像?

public string GetTickerData() 
     { 
      string strData = ""; 
      string strImageData = ""; 
      string strDateFormat = ""; 
      byte[] imgdata; 
      int s = 0; 
      strImageData = "";         
      strImageData += "<img runat=\"server\" src=\"TickerImage.ashx?ColName=" + dtTickerData.Columns[j].ColumnName + "&ID=" + s + "\" />"; 
      ViewData["Data"]=strImageData ; 
     } 

Image.cshtml

@{ 
    var t= @ViewData["Data"]; 
} 
<div> 
<label class="footerLabel" style="text-align:center">@ViewData["Data"]</label> 
</div> 

圖像處理

public class TickerImage : IHttpHandler 
    { 
     string ColumnName = string.Empty; 
     int RowID = 0; 
     public void ProcessRequest(HttpContext context) 
     { 
      if (context.Request.QueryString["ColName"] != null) 
       ColumnName = context.Request.QueryString["ColName"]; 

      if (context.Request.QueryString["ID"] != null) 
       RowID = Convert.ToInt32(context.Request.QueryString["ID"]); 
      else 
       RowID = 0; 

      DataTable dtTemp = (DataTable)context.Session["TickerData"]; 

      if (!(dtTemp.Rows[RowID][ColumnName] is DBNull)) 
       context.Response.BinaryWrite((byte[])dtTemp.Rows[RowID][ColumnName]); 
     } 


    } 

回答

1

它看起來不錯,但你缺少一個右引號。

strImageData += "<img src=\"" + imgg + " />"; 

這應該是:

strImageData += "<img src=\"" + imgg + "\" />"; 
+0

沒有區別的。 –

相關問題