帮助从列表框中打开图像 [英] Help with opening image from listbox

查看:65
本文介绍了帮助从列表框中打开图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我解释一下我的目标.
我想使用folderBrowserDialog在列表框中打开文件的整个文件夹.我能够做到.但是,我还想从列表框本身中选择文件,并且在选择后,我需要将图像显示在图片框中.
现在,使用下面的代码,我只能打开文件夹并显示列表框中的所有文件.它无法在图片框中显示单个图像.
如果您听不懂我想说的话,请发表评论并让我知道.谢谢:)

let me explain what is my aim.
i would like to open whole folder of files in a listbox using the folderBrowserDialog. I am able to do that. But, i also want to select the file from the listbox itself and upon selecting, i need the image to appear in the picturebox.
Now, with my code below, i''m only able to open the folder and display all the files in the listbox. It''s not able to appear a single image in the picturebox.
If u''re not able to understand what i''m trying to say, please comment and let me know. Thank You:)

DialogResult results = folderBrowserDialog1.ShowDialog();
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
     listBox1.Items.Clear(); //Clear items in listbox1
     string folderName = this.folderBrowserDialog1.SelectedPath;   //To browse folder
     DirectoryInfo dir = new DirectoryInfo(folderName);
     FileInfo[] fileInfo = dir.GetFiles("*.*");                    //Get only files in .jpg form if GetFiles("*.jpg")
     foreach (FileInfo f in fileInfo)
     {
         listBox1.Items.Add(f);  //add files from the folder into listbox1
     }
     pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); //Returns an image object located at the path specified
}


错误发生在:
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); //返回位于指定路径的图像对象

一百万个谢谢!!
干杯!! :)


error occurs at:
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); //Returns an image object located at the path specified

A Million Thanks!!
Cheers!! :)

推荐答案

是的,您在哪里设置openFileDialog1.FileName,

首先,您的代码似乎两次调用了folderBrowserDialog1.ShowDialog,而没有调用openFileDialog1的ShowDialog.

您应选择一个文件,否则应选择要为其加载图片的FileInfo对象,然后选择该文件.
Yes, where did you set openFileDialog1.FileName,

First of all your code seems that you are calling folderBrowserDialog1.ShowDialog twice and didnt called ShowDialog of openFileDialog1.

You should select a file, or otherwise you should gt the FileInfo object for which you want to load the picture and select the file.


为此,您需要使用listBox1.SelectedItem.

记住,您需要再次将其转换为FileInfo.
For that you need to use listBox1.SelectedItem.

Remember, you need to convert it to FileInfo again.


这篇关于帮助从列表框中打开图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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