MxLDevs
MxLDevs

Reputation: 19546

C# selecting an image and drawing it in a panel

I have a simple list of filenames of (supported) images in a listbox. When I select a filename I want to have the image drawn in a panel (like a preview).

How do I access the panel to actually load the image?

Upvotes: 0

Views: 120

Answers (2)

JMK
JMK

Reputation: 28069

Add this to the SelectedIndexChanged event handler of your listbox. You can find this by clicking on your listbox, looking in the properties pane, clicking on the lightning bolt and double clicking the blank space beside SelectedIndexChanged:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var currentImageLocation = listBox1.SelectedItem.ToString();
    Image myImage = Image.FromFile(currentImageLocation);
    panel1.BackgroundImage = myImage;
}

You obviously need to update the generic names above to the ID's of your listbox and panel

Upvotes: 2

yogi
yogi

Reputation: 19619

Simply get file's location on listbox's item selection and set panel's background image property to that location.

Upvotes: 0

Related Questions