![](/img/trans.png)
[英](C#, WinForms) How to assign an accessibility attribute to an image in ImageList
[英]How can I replace an existing image on a winforms ImageList?
如何替換Winforms ImageList
上的現有圖像?
我嘗試了這個:
this.CoolPics.Images [ 2 ] = // new image
this.ListViewControl.SmallImageList = this.CoolPics;
但是,當我使用this.CoolPics.Images.Add
方法時,新圖像不會像其他圖像那樣重新縮放。
我究竟做錯了什么?
我知道這很老,但是這是我解決問題的方法。 看起來圖像列表在分配時不會調整圖像的大小(即使使用Add()函數時也是如此)。 因此,基本上,您需要在分配之前手動調整圖像的大小。
Image img; //used to load new image from disk
Bitmap bmp = new Bitmap(160, 120); //canvas where the new image will be drawn/resized
Graphics graph = Graphics.FromImage(bmp); //used to draw/resize the new image
img = new Bitmap(fileDialog.FileNames[0]); //load new image from disk
graph.DrawImage(img, new Rectangle(0, 0, 160, 120)); //resize new image to proper size
imgList.Images[index] = bmp; //assign the new resized image to the list (overwrites the old image)
您的代碼后嘗試
listView1.Refresh();
我之前碰到過這個問題,如果我沒記錯的話,賦值運算符也有這種行為,但是Imagelist.Images.Add(myImage)做正確的事情。
嘗試更改代碼以執行.Add(myImage),看看效果是否更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.