Ask Your Question
1

How can I show an image from the database in my picture box?

asked 2022-03-29 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-07-17 12:00:00 +0000

woof gravatar image

Assuming you're using C# and Windows Forms, you can follow these steps:

  1. Retrieve the image from the database as a byte array.

  2. Convert the byte array into a MemoryStream.

    MemoryStream ms = new MemoryStream(byteArray);

  3. Use the FromStream static method of the Image class to create an Image object from the MemoryStream.

    Image image = Image.FromStream(ms);

  4. Set the Image property of the PictureBox control to the Image object.

    pictureBox1.Image = image;

Here is an example code snippet:

// Assuming connection is a SqlConnection object
SqlCommand command = new SqlCommand("SELECT ImageColumn FROM Table WHERE ID=1", connection);
byte[] byteArray = (byte[]) command.ExecuteScalar();
MemoryStream ms = new MemoryStream(byteArray);
Image image = Image.FromStream(ms);
pictureBox1.Image = image;
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-03-29 11:00:00 +0000

Seen: 12 times

Last updated: Jul 17 '21