【问题标题】:Showing an image in listview from datatable在数据表的列表视图中显示图像
【发布时间】:2012-04-10 19:45:39
【问题描述】:

我有一个带有图像类型列的 SQL Server 2008 表,我用它来存储图像(在 SQL 图像类型中)。

我有一个 ASPX 页面,上面有一个列表视图,它绑定到一个数据表,该数据表是从包含该图像的查询中填充的。填充数据表时获取图像字段的值。

现在我想显示该列表视图的每个字段的图像。我使用<%# Eval("ColumnA") %> like 语法来显示从该查询中获取的文本,但我不知道如何显示来自该查询的图像。 (例如,使用哪个控件?使用哪个语法?)

【问题讨论】:

  • 此链接显示了一个示例:aspsnippets.com/Articles/…
  • 我不是在寻找将图像 URL 保存在数据库中而不是图像本身的解决方案。
  • 谢谢乔希,但我正在寻找一种涉及 Eval 表达的方法。

标签: c# asp.net listview


【解决方案1】:

试试Data URL scheme

<img src="<%# ReturnEncodedBase64UTF8(Eval("ColumnA")) %>" />

protected static string ReturnEncodedBase64UTF8(object rawImg)
{
    string img = "data:image/gif;base64,{0}"; //change image type if need be
    byte[] toEncodeAsBytes = (byte[])rawImg;        
    string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
    return String.Format(img, returnValue);
}

【讨论】:

  • 无法将“System.Byte[]”类型的对象转换为“System.String”类型。
  • 因为我需要做一个:System.Text.Encoding.UTF8.GetBytes((string)rawImg);
  • 伙计,它成功了 :) 你让我开心。非常非常非常感谢您的帮助。这个问题让我很头疼。谢谢你,我的朋友。
猜你喜欢
  • 2013-05-18
  • 1970-01-01
  • 2013-03-26
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多