【问题标题】:Displaying an image from SQL Server in asp.net在 asp.net 中显示来自 SQL Server 的图像
【发布时间】:2011-09-27 13:01:42
【问题描述】:

我需要在我的 asp.net 页面上显示一个图像,该图像在 SQL Server 中存储为图像数据类型。我正在看这篇帖子,display/retrieve image from sql database in vb.net,但是,如何在<td> 元素中显示该图像?

【问题讨论】:

    标签: asp.net sql-server vb.net image


    【解决方案1】:

    <td> 标签不能用于显示图像,除非您在其中放入 <img> 元素。话虽如此,这就是您在 VB.NET 中从后面的代码显示图像的方式:

    在您的 aspx 页面中有这种类型的标记:

    <td> 
    <img src="" runat="server" id="imageFromDB" />
    ... 
    

    您可以在后面的代码中执行此操作:

    Dim imageBytes() as Byte= ...  // You got the image from the db here...
    //jpg is used as an example on the line below. 
    //You need to use the actual type i.e. gif, jpg, png, etc.
    //You can do imageFromDB simply because you set runat="server" on the markup
    imageFromDB.src = String.Format("data:image/{0};base64,
    {1}","jpg",Convert.ToBase64String(imageBytes)
    

    这将在页面上呈现您的图像。

    我希望我在这里给了你足够的信息。

    【讨论】:

      【解决方案2】:

      参考您提到的答案中的示例:

      <td><img src="PathToYourHttpHandler?id=ID_OF_YOUR_IMAGE" /></td>
      

      【讨论】:

        猜你喜欢
        • 2018-12-03
        • 1970-01-01
        • 2013-12-03
        • 2021-11-18
        • 2012-01-06
        • 1970-01-01
        • 1970-01-01
        • 2016-10-07
        • 2010-10-11
        相关资源
        最近更新 更多