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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2175209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:08:17  来源:igfitidea点击:

Retrieve Images from sql server database

c#sql-server-2005image

提问by Geeth

i am storing images to the database. How to retrieve all the images from the database.

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

Eg: select images from imagetable

例如:从 imagetable 中选择图像

Problem:

问题:

Data Logic:

数据逻辑:

           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 page:

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>");
}

Image not getting displayed

图像未显示

Geetha

吉塔

采纳答案by Yevhen

this is an example from Sql Server

这是来自 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();

look here http://www.akadia.com/services/dotnet_read_write_blob.html

看这里 http://www.akadia.com/services/dotnet_read_write_blob.html

回答by Mitch Wheat

For SQL Server 2008 onwards, FILESTREAM is almost certainly the way to go.

对于 SQL Server 2008 以后的版本,FILESTREAM 几乎肯定是要走的路。

Please see: SQL Server 2005 - How do I convert image data type to character format

请参阅:SQL Server 2005 - 如何将图像数据类型转换为字符格式

回答by Fitzchak Yitzchaki

You need to get the binary data from the DB, and then stream the binary data to the browser as image.

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

回答by cjk

You are setting the Url of the image to be the byte stream - you need to save the image to the hard drive and provide the location.

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

A much better way would be to set the image url to be a resource handler with parameters that could then retrieve the image from the database and return it as a stream to the browser.

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