C# open image path from listBox and show image in PictureBox

I have a listBox where I save paths from Images. We save the paths to sql Database. This works well. My question is, is there a way to click in ListBox on one of the paths and show the image in a PictureBox? Would be nice with a mouse click event in ListBox.

enter image description here

Upvotes: 0

Views: 1591

Answers (1)

slow
slow

Reputation: 562

Double click on your ListBox and it should create you a new method.

Paste this code:

string path = listBox1.SelectedItem.ToString();

pictureBox1.Image = Image.FromFile(path);

EDIT:

Like Jimi said in the comments, the above code will lock the file.

Use this code instead:

using (Bitmap tmpBitmap = new Bitmap(listBox1.SelectedItem.ToString()))
{
    pictureBox1.Image = new Bitmap(tmpBitmap);
}

Upvotes: 1

Related Questions