C#从listBox打开图像路径并在PictureBox中显示图像 [英] C# open image path from listBox and show image in PictureBox

查看:669
本文介绍了C#从listBox打开图像路径并在PictureBox中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个listBox,我在其中保存了Images的路径.我们将路径保存到sql数据库.这很好.我的问题是,有没有办法在其中一个路径上单击ListBox并在PictureBox中显示图像?在ListBox中使用鼠标单击事件会很好.

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.

推荐答案

双击ListBox,它将为您创建一个新方法.

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

粘贴此代码:

string path = listBox1.SelectedItem.ToString();

pictureBox1.Image = Image.FromFile(path);

就像吉米在评论中说的那样,以上代码将锁定文件.

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

改为使用此代码:

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

这篇关于C#从listBox打开图像路径并在PictureBox中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆