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]);
}
}
沒有區別的。 –