用新图像替换imagelist项目 [英] replace imagelist item with a new image

查看:260
本文介绍了用新图像替换imagelist项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你不能在你自己的帖子上投票

0



我在点击按钮时使用以下代码填写了一个图像列表。一切正常。



 DirectoryInfo dir =  new  DirectoryInfo( @  c:\ MyPy); 
foreach (FileInfo文件 in dir.GetFiles())
{
imageList1.Images.Add(Image.FromFile(file.FullName));
}

listView1.View = View.LargeIcon;
imageList1.ImageSize = new 大小( 100 100 );
listView1.LargeImageList = this .imageList1;
ListViewItem项目;
for int i = 0 ; i < this .imageList1.Images.Count; i ++)
{
item = new ListViewItem();
item.ImageIndex = i;
item.Text = Image + i.ToString();
listView1.Items.Add(item);
}







但是当我更换图像列表中的任何图像并刷新列表框时然后不显示完整图像,显示图像的一部分。代码如下:



 imageList1.Images [listView1.FocusedItem.ImageIndex] = img;  //  这里我替换特定地点的图像 

listView1.Refresh ();





我想我必须将imagelayout更改为拉伸。但我怎么能这样做?以及为什么第一次显示完整图像?

解决方案

看看 ObjectListView [ ^ ]



它让你想做的事情变得更容易:)



在你的情况下,我想我使用所有者绘图 [ ^ ]。



最好的问候

Espen Harlinn


这是解决这个问题的方法:

1.按键从图像集中删除图像。

2.使用相同的键向图像集添加新图像。



图片newImage = ...  //  新图片 
string imgKey = ... // 要更新的图像的关键
// 找到上一张图片key
Image previousImage = imgList.Images [imgList.Images.Keys.IndexOf( imgKey )];
// 从集合中删除上一张图片
imgList.Images.RemoveByKey (imgKey);
// 处理上一张图片
previousImage.Dispose();
// 添加新图片
imgList.Images.Add(imgKey, newImage);


You cannot vote on your own post
0

I have filled an imagelist using the following code in a button click. everything works fine.

DirectoryInfo dir = new DirectoryInfo(@"c:\MyPic");
foreach (FileInfo file in dir.GetFiles())
{
imageList1.Images.Add(Image.FromFile(file.FullName));
}

listView1.View = View.LargeIcon;
imageList1.ImageSize = new Size(100, 100);
listView1.LargeImageList = this.imageList1;
ListViewItem item;
for (int i = 0; i < this.imageList1.Images.Count; i++)
{
    item = new ListViewItem();
    item.ImageIndex = i;
    item.Text = "Image " + i.ToString();
    listView1.Items.Add(item);
}




But when i replace any image in the imagelist and refresh the list box then the full image is not displayed, a portion of the image is displayed. Codes are as follows:

imageList1.Images[listView1.FocusedItem.ImageIndex] = img;  // here I replace the image at a specific place
 
listView1.Refresh();



I think I have to change the imagelayout as stretch. But how could i do that? and why the full image is displayed at first time?

解决方案

Have a look at ObjectListView[^]

It makes what you are trying to do a lot easier :)

In your case I think I'd use owner drawing [^].

Best regards
Espen Harlinn


This is the way how to solve this problem:
1. Remove the image from the image collection by key.
2. Add a new image with the same key to the image collection.

Image newImage = ... // new image
string imgKey = ... // the key of the image to update
// find the previous image by key
Image previousImage = imgList.Images[imgList.Images.Keys.IndexOf("imgKey")];
// remove the previous image from the collection
imgList.Images.RemoveByKey(imgKey);
// dispose the previous image
previousImage.Dispose();            
// add a new image
imgList.Images.Add(imgKey, newImage);


这篇关于用新图像替换imagelist项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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