0

I trying to show images from SQL Server 2008 in a PictureBox of C#.Net Windows Form.

I don't know how to retrieve images from SQL Server and don't know how to show in the PictureBox. Please write a code segment for me if you Okay...

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Titan
  • 29
  • 1
  • 3
  • SO is not for "please write my code for me" kinds of questions. What are you having difficulties with exactly ? Try breaking down the problem in smaller tasks, then ask a specific question if you get stuck. – driis Jun 26 '11 at 16:36
  • But I exactly don't know how to show in PictureBox of C#.net. – Titan Jun 26 '11 at 16:39
  • 1
    Did you search first? For example, a basic google returns: http://www.codeproject.com/KB/database/ImageSaveInDataBase.aspx – Marc Gravell Jun 26 '11 at 16:42
  • 1
    Relates questions include: http://stackoverflow.com/questions/2382108/winforms-how-to-upload-a-picture-into-sql-server-database-using-c http://stackoverflow.com/questions/912062/how-to-save-picture-in-the-database http://stackoverflow.com/questions/4349528/how-to-show-picture-that-is-stored-in-sql-server-database-using-asp-net – Marc Gravell Jun 26 '11 at 16:43

1 Answers1

4

You haven't specified how are you accessing this database, so I assume you are using plain ADO.NET. In this case you could have a method which would query your database and return the image as a byte array:

public byte[] GetImageData(int imageId)
{
    using (var connection = new SqlConnection(SomeConnectionString))
    using (var command = connection.CreateCommand())
    {
        connection.Open();
        command.CommandText = "SELECT image_data FROM images WHERE image_id = @imageId";
        command.Parameters.AddWithValue("@imageId", imageId);
        using (var reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                const int CHUNK_SIZE = 2 * 1024;
                byte[] buffer = new byte[CHUNK_SIZE];
                long bytesRead;
                long fieldOffset = 0;
                using (var stream = new MemoryStream())
                {
                    while ((bytesRead = reader.GetBytes(0, fieldOffset, buffer, 0, buffer.Length)) > 0)
                    {
                        stream.Write(buffer, 0, (int)bytesRead);
                        fieldOffset += bytesRead;
                    }
                    return stream.ToArray();
                }
            }
        }
    }
    throw new Exception("An image with id=" + imageId + " was not found");
}

and then load it into a picture box:

private void Form1_Load(object sender, EventArgs e)
{
    var pb = new PictureBox();
    using (var stream = new MemoryStream(GetImageData(1)))
    {
        pb.Image = Image.FromStream(stream);
    }
    Controls.Add(pb);
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks for the solution. The OP did assert "I don't know", twice: the solution is more appropriate than the 'lecture'. – AAsk Jun 26 '11 at 17:19