【问题标题】:display varbinary image in asp.net with c#使用 c# 在 asp.net 中显示 varbinary 图像
【发布时间】:2013-06-05 18:22:49
【问题描述】:

所以我有这个方法:

    string sql = string.Format("EXECUTE getPlayerImage @playerID = '{0}'", bio.playerID);


    SqlCommand cm = baseballConnection.CreateCommand();

    baseballConnection.Open();
    cm.CommandText = sql;
    baseballConnection.Close();
    return cm.ExecuteScalar() as byte[];

它有效,至少我没有错误。但现在我只有一个字节数组,我怎样才能在屏幕上显示它?我在屏幕上有一个图像工具,但我知道如何显示图像的唯一方法是它是否保存在服务器上并具有文件路径。我不知道如何将字节数组保存到服务器,但这会很麻烦,而且我已经将图像作为 varbinary 保存在数据库中并现在检索它是没有意义的。

我怎样才能让这个字节数组显示?

【问题讨论】:

  • 我宁愿让服务器从硬盘传输文件,也不愿让它启动臃肿的 asp.net 生命周期,访问数据库,通过内存传递数据,然后传输文件。
  • 我明白了。我只是想看看我能不能做到这一点。我已经走了这么远,我知道这更容易,但尽管如此,我希望看到这成功

标签: c# asp.net image varbinary


【解决方案1】:

要在 asp.net 中呈现图像,您必须创建一个 asp.net 页面或通用处理程序来保存输出响应流中的字节。

然后您将设置 HTML 图像或控件以链接到页面或通用处理程序。

例如,

<img src="getimage.aspx?id=1" style="width:100px; height:100px" alt="" />

所以在你的情况下,它会是这样的:

using System.Web;

namespace ImageUtil
{
    public class ImageProvider : IHttpHandler
    {
        publicvoid ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";

            string sql = string.Format("EXECUTE getPlayerImage @playerID = '{0}'", bio.playerID);

            SqlCommand cm = baseballConnection.CreateCommand();
            baseballConnection.Open();
            cm.CommandText = sql;
            byte[] img = (byte[])cm.ExecuteScalar();
            baseballConnection.Close();

            context.Response.BinaryWrite(img);
        }

        publicbool IsReusable
        {
            get
            {
                returnfalse;
            }
        }
    }
}

这是一个链接: http://samiradel.wordpress.com/2011/08/03/how-to-display-image-byte-array-in-a-an-img-tag/

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 2021-11-20
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多