问题描述
谁能举个例子,说明如何清除列表框中显示的图像.
我在表单上有列表框和两个按钮.
当我单击第一个按钮(以选择图像文件夹的路径)时.第二个按钮在列表框中显示图像.为此,我给了一个ImagesList.tal,所有图像都显示在列表框中.
现在,当我选择其他图像文件夹,然后单击第二个按钮时,列表框中的所有图像应清除并显示下一个选定的文件夹图像.
错误:该进程无法访问该文件(在文件夹中显示第一个图像名称),因为该文件正在被另一个进程使用.
我想要当我第二次单击显示图像"按钮时,它应该包含列表框中显示的所有图像,并在列表框中显示选定的文件夹图像.
Hi,
can any one give example how to clear images displayed in a listbox.
I have listbox and two buttons on the form.
when i click on first button(to select path of the images folder). and second button to display images in the listbox. for this i have talen a ImagesList.And all the images are displaying in Listbox.
Now when i select the other images folder, and click on second button all the images from the listbox should clear and display the next selected folder images.
error: The process cannot access the file(showing first image name in the folder) because it is being used by another process.
I want when i click on show images button second time it should claer all he images from the listbox displaying and display the selected folder images in the listbox.
Showimages button Code:
listbox1.itemssource=null;
listbox1.items.Clear();
List<bitmapimages> imageslist=new List<bitmapimages>
System .IO .DirectoryInfo myimagesdir=new System.IO.DirectoryInfo("selected directory name");
foreach(System.IO.FileInfo myimagesfile in myimagesdir.GetFiles("*.jpg"))
{
...
...
}
listbox1 .ItemsSource =imageslist;
</bitmapimages></bitmapimages>
谢谢,
Thanks,
推荐答案
if (isFirst)
{
listBox1.DataSource = null;
listBox1.Items.Clear();
var imageslist = new List<string>();
var myImagesDir = new System.IO.DirectoryInfo(@"dir path");
foreach (System.IO.FileInfo myimagesfile
in myImagesDir .GetFiles("*.jpg"))
{
imageslist.Add(myimagesfile.Name);
}
listBox1.DataSource = imageslist;
isFirst = false;
}
else
{
listBox1.DataSource = null;
isFirst = true;
}</string>
foreach(FileInfo file in files){
FileStream fs = file.OpenRead();
MemoryStream ms = new MemoryStream();
fs.CopyTo(ms);
fs.Close(); // this frees the file
Image image = Image.FromStream(ms); // note: NOT ''fs'' or Image.FromFile
}
这篇关于从C#中的列表框中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!