![](/img/trans.png)
[英]How to make automated image slideshow using imagelist & timer in C#?
[英]C# How to check if a PictureBox contains an image in a specified imagelist?
我需要知道PictureBox
是否在指定的imageList
包含一个Image
。 因为如果ImageList长,如果imageList
中的每个图像都在PictureBox中, if-statement
在if-statement
要花很多时间。 当我键入
foreach (var image in imageList1)
{
if (pictureBox1.Image = image)
{
//Code
}
}
它不起作用:/
请尝试以下操作:
//using System.Linq;
bool isinside = /*yourImageList*/.Images.Any(x => x == /*yourPictureBox*/.Image);
if (isinside)
{
//is in yourImageList
MessageBox.Show("image is from imagelist!");
//or do what you want here!
}
ImageList
是一件特殊的事情,它可能是正确对象,也可能不是正确对象。
一件好事是它存储的图像不是Image
类型的; 这样很好,因为即使您添加了大量图像,它也不需要为其存储的每个图像使用GDI
句柄。
这样做的一个问题是,您根本无法将其图像与分配给PictureBox.Image
的图像进行比较。
因此,即使在更正了语法之后,此操作也将无法进行:
// assign an image
pictureBox1.Image = imageList1.Images[2];
// now look for it:
foreach (var img in imageList1.Images)
{
if (pictureBox1.Image == img )
{
Console.WriteLine("Found the Image!"); // won't happen
}
}
相反,您应该将图像的Key
存储在ImageList
:
pictureBox1.Image = imageList1.Images[2];
pictureBox1.Tag = imageList1.Images.Keys[2];
现在,您可以轻松地寻找关键:
foreach (var imgKey in imageList1.Images.Keys)
{
if (pictureBox1.Tag.ToString() == imgKey )
{
Console.WriteLine("Found the Image-Key!"); // will happen quite quickly!
}
}
当然,首先使用ImageList
是否是个好主意是另一个问题。
它实际上是与ListViews
, TreeViews
以及其他可以显示小图标图像的控件一起使用的。
例如,限制为256x256像素; 还:其所有图像必须具有相同的尺寸!
但是,如果可以,请继续使用。
否则,如Hans所建议的那样, List<Image>
是一种自然而灵活的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.