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

邮差的信 提交于 2019-12-24 23:45:26

问题


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.


回答1:


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);
}


来源:https://stackoverflow.com/questions/48785895/c-sharp-open-image-path-from-listbox-and-show-image-in-picturebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!