Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从sql server数据库检索图像_C#_Sql Server 2005_Image - Fatal编程技术网

C# 从sql server数据库检索图像

C# 从sql server数据库检索图像,c#,sql-server-2005,image,C#,Sql Server 2005,Image,我正在将图像存储到数据库中。如何从数据库中检索所有图像 例如:从imagetable中选择图像 问题: 数据逻辑: while (dr.Read()) { ///Check for null if (!dr.IsDBNull(0)) { try { ///Convertin

我正在将图像存储到数据库中。如何从数据库中检索所有图像

例如:从imagetable中选择图像

问题:

数据逻辑:

           while (dr.Read())
          {
             ///Check for null
              if (!dr.IsDBNull(0))
             {
                try
                {
                    ///Converting the image into bitmap
                    byte[] photo = (byte[])dr[0];
                    ms = new MemoryStream(photo);
                    Bitmap bm = new Bitmap(ms);
                    bmp[i] = bm;

                    ms.Close();

                }
                catch (Exception ex)
                {

                }
            }
ASpx.CS页面:

Bitmap[] bm= photos.GetImage();
    for (int i = 0; i < bm.Length; i++)
    {
  MemoryStream ms = new MemoryStream();

        **bm[i].Save(ms, ImageFormat.Jpeg);**(Error : A generic error occurred in GDI+.)

htmlCode.Append("<li><img ImageUrl=\\\"");
htmlCode.Append(**ms.GetBuffer()**);
htmlCode.Append("\" alt=\"\" width=\"100\" height=\"100\"></li>");
}
Bitmap[]bm=photos.GetImage();
对于(int i=0;i”);
}
图像无法显示


Geetha

对于SQL Server 2008以后的版本,FILESTREAM几乎可以肯定是一条出路


请参阅:

您需要从数据库获取二进制数据,然后将二进制数据作为图像流式传输到浏览器。

这是Sql Server的一个示例

        connection.Open();
        SqlCommand command1 = new SqlCommand("select imgfile from myimages where imgname=@param", connection);
        SqlParameter myparam = command1.Parameters.Add("@param", SqlDbType.NVarChar, 30);
        myparam.Value = txtimgname.Text;
        byte[] img = (byte[])command1.ExecuteScalar();
        MemoryStream str = new MemoryStream();
        str.Write(img, 0, img.Length);
        Bitmap bit = new Bitmap(str);
        connection.Close();
看这里

您正在将图像的Url设置为字节流-您需要将图像保存到硬盘并提供位置


更好的方法是将图像url设置为一个资源处理程序,其参数可以从数据库中检索图像并将其作为流返回到浏览器。

不要将图像存储到数据库中。别忘了问你的问题。你在检索数据或将二进制数据显示为图像时有问题吗?@aaronnout-除非你使用的是FILESTREAM。它在管理和一致性方面有一些巨大的优势。@womp:Agree,我通常会明确指出这一点,但它被标记为
sql-server-2005
,它没有
FILESTREAM
)bm[i].Save(ms,ImageFormat.Jpeg);(错误:GDI+中发生了一个一般性错误)您能用一个例子解释一下吗。