none
how to get pictures from database RRS feed

  • Question

  •  

          i have a problem with data store in database is picture, when i read it in to byte array, i dont know how to convert it into image to display in control (example picturebox). can some one help me? thanks very much.
    Wednesday, January 16, 2008 11:13 AM

All replies

  • Hi,

    This should do the trick:

    Code Block

    Image image;
    byte
    [] byteArray;

    byteArray = GetImageByteArray();

    image = Image.FromStream(new MemoryStream(byteArray));


    Response.Clear();

    Response.ContentType = "image/jpeg";

    image.Save(Response.OutputStream, ImageFormat.Jpeg);

    Response.End();

    image.Dispose();

     

    Hope this helps.

    Wednesday, January 16, 2008 12:17 PM
  • thank you very much, but it not work. here my testing code:

    private ArrayList arrImage = new ArrayList();

    void GetImage()

    {

    SqlConnection connect = new SqlConnection("server=.\\sqlexpress;database=NorthWind;Integrated Security=SSPI;");

    try

    {

    connect.Open();

    SqlCommand command = new SqlCommand("select Pisture from Categories", connect);

    SqlDataReader reader = command.ExecuteReader();

    while(reader.Read())

    {

    byte[] img = new byte[10];

    reader.GetBytes(0, 0, img, 0, 10);

    arrImage.Add(img);

     

    }

    }

     

    }

     

    the code written for window application, not for web. i wonder how to convert byte stream to image to display in control such as PictureBox control.

     

    Wednesday, January 16, 2008 1:01 PM
  • I just tried this myself using the Northwind images and it does not work. The format of the binary image in the Northwind database is not directly supported by the Image.FromStream helper.

     

    This is not a problem for you unless your intention is to read the Northwind database. Since it's a training database and not a real-world application, that is unlikely.

     

    Use the steps outlined in the link I gave above to read and write your own images.

    Wednesday, January 16, 2008 3:39 PM
  • thank tergiver very much

    Wednesday, January 16, 2008 6:10 PM
  • This does work with the AdventureWorks sample (see SQL Server 2005 Samples). I tested it out with the following code. The form contains a ListBox and a PictureBox.

     

    Code Block

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.IO;

    namespace DbTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                PhotoInfo info = (PhotoInfo)listBox1.SelectedItem;
                using (SqlConnection con = new SqlConnection(@"server=680I\SQLEXPRESS;initial catalog=AdventureWorks;Integrated Security=SSPI;"))
                {
                    con.Open();
                    using (SqlCommand command = new SqlCommand("select LargePhoto from Production.ProductPhoto where ProductPhotoID=" + info.ID.ToString(), con))
                    {
                        byte[] raw = (byte[])command.ExecuteScalar();
                        using (MemoryStream stream = new MemoryStream(raw))
                            pictureBox1.Image = Image.FromStream(stream);
                    }
                }
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                using (SqlConnection con = new SqlConnection(@"server=680I\SQLEXPRESS;initial catalog=AdventureWorks;Integrated Security=SSPI;"))
                {
                    con.Open();
                    using (SqlCommand command = new SqlCommand("select ProductPhotoID, LargePhotoFileName from Production.ProductPhoto", con))
                    {
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                PhotoInfo info = new PhotoInfo((int)reader[0], (string)reader[1]);
                                listBox1.Items.Add(info);
                            }
                        }
                    }
                }
            }
        }

        public class PhotoInfo
        {
            public int ID;
            public string Name;
            public PhotoInfo(int id, string name)
            {
                ID = id; Name = name;
            }
            public override string ToString()
            {
                return Name;
            }
        }
    }

     

     

    Wednesday, January 16, 2008 7:10 PM
  • Curiously, AdventureWorks does not use the image data type to store the raw image data. It uses varbinary(max). No idea why though.

     

    Wednesday, January 16, 2008 7:15 PM