有没有办法确定两个图像是否相同?我想在我的计时器滴答作响(动画)的时候改变一个图像。但是,我需要看到哪个图像正在显示,所以有没有办法比较两个图像来做我想要的?
if (myImage.Flags == (Image.FromFile(@"Images/Enemy.png").Flags))
{
myImage = Image.FromFile(@"Images/Enemy2.png");
}
else
{
myImage = Image.FromFile(@"Images/Enemy.png");
}发布于 2014-02-05 23:31:22
不要比较图像,只需在变量中维护当前图像的索引。
下面是一个适用于任意数量的图像的示例:
private int _currentImageIndex;
private string[] _imagePaths =
{
"Images/Enemy.png",
"Images/Enemy2.png",
"Images/Enemy3.png",
};
...
void NextImage()
{
// Dispose the current image
Image img = pictureBox1.Image;
pictureBox1.Image = null;
if (img != null)
img.Dispose();
// Show the next image
_currentImageIndex = (_currentImageIndex + 1) % _imagePaths.Length;
string path = _imagePaths[_currentImageIndex];
pictureBox1.Image = Image.FromFile(path);
}发布于 2014-02-05 23:29:33
我会试着比较一下ImageLocation。尽管如果你有你的图片作为资源,它就不起作用了。
if (PictureBox1.ImageLocation == PictureBox2.ImageLocation)
{
}发布于 2014-02-05 23:51:28
这是一个简单的答案。
如果只有2个图像,请使用flag
// field, true if enemy2.png is loaded
bool _image2;
// somewhere
if(_image2)
{
myImage = Image.FromFile(@"Images/Enemy.png");
_image2 = false;
}
else
{
myImage = Image.FromFile(@"Images/Enemy2.png");
_image2 = true;
}https://stackoverflow.com/questions/21581233
复制相似问题