如何检查图像对象是否与资源中的对象相同?

因此,我正在尝试创建一个简单的程序,只需在单击时更改图片框中的图片即可。 我目前只使用两张图片,所以我的图片框点击事件function的代码如下所示:

private void pictureBox1_Click(object sender, EventArgs e) { if (pictureBox1.Image == Labirint.Properties.Resources.first) pictureBox1.Image = Labirint.Properties.Resources.reitmi; else if (pictureBox1.Image == Labirint.Properties.Resources.reitmi) pictureBox1.Image = Labirint.Properties.Resources.first; } 

由于某种原因if语句不起作用,图片不会改变。 我该怎么办?


注意:原始代码包含第二个错误, if第一个条件的撤消效果与Cyral的答案建议的修复一起使用,但添加else没有解决问题 – 单步执行代码仍显示没有匹配任何一个图像。

 if (pictureBox1.Image == Labirint.Properties.Resources.first) pictureBox1.Image = Labirint.Properties.Resources.reitmi; if (pictureBox1.Image == Labirint.Properties.Resources.reitmi) // was missing else pictureBox1.Image = Labirint.Properties.Resources.first; 

  if (pictureBox1.Image == Labirint.Properties.Resources.first) 

这里有一个陷阱,没有足够的.NET程序员知道。 负责许多以臃肿的记忆足迹运行的程序。 使用Labirint.Properties.Resources.xxxx属性创建一个新的图像对象,它永远不会匹配任何其他图像。 您只需要使用该属性一次,将图像存储在类的字段中。 大致:

  private Image first; private Image reitmi; public Form1() { InitializeComponent(); first = Labirint.Properties.Resources.first; reitmi = Labirint.Properties.Resources.reitmi; pictureBox1.Image = first; } 

现在你可以比较它们:

  private void pictureBox1_Click(object sender, EventArgs e) { if (pictureBox1.Image == first) pictureBox1.Image = reitmi; else pictureBox1.Image = first; } 

并避免内存膨胀:

  private void Form1_FormClosed(object sender, FormClosedEventArgs e) { first.Dispose(); reitmi.Dispose(); } 

你需要使用else if ,因为如果图像是first ,你将它设置为reitmi ,那么你检查它是否是reitmi ,它现在是,然后将它改回到first 。 这最终根本没有变化。

 if (pictureBox1.Image == Labirint.Properties.Resources.first) pictureBox1.Image = Labirint.Properties.Resources.reitmi; else if (pictureBox1.Image == Labirint.Properties.Resources.reitmi) pictureBox1.Image = Labirint.Properties.Resources.first; 

也许这段代码可能有点大,但对我来说效果很好,试试吧:

这是讽刺:

 using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; using System.Collections; 

以下是要比较的代码:

  // Your Images Image img1 = pictureBox1.Image; Image img2 = pictureBox2.Image; // Now set as bitmap Bitmap bmp1 = new Bitmap(img1); Bitmap bmp2 = new Bitmap(img2); // here will be stored the bitmap data byte[] byt1 = null; byte[] byt2 = null; // Get data of bmp1 var bitmapData = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp1.PixelFormat); var length = bitmapData.Stride * bitmapData.Height; // byt1 = new byte[length]; // Marshal.Copy(bitmapData.Scan0, byt1, 0, length); bmp1.UnlockBits(bitmapData); // Get data of bmp2 var bitmapData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp2.PixelFormat); var length2 = bitmapData2.Stride * bitmapData2.Height; // byt2 = new byte[length2]; // Marshal.Copy(bitmapData2.Scan0, byt2, 0, length2); bmp2.UnlockBits(bitmapData2); // And now compares these arrays if (StructuralComparisons.StructuralEqualityComparer.Equals(byt1, byt2)) { MessageBox.Show("Is Equal"); } else { MessageBox.Show("Isn`t equal"); } 

此代码比较每个字节图像以生成结果。 可能有其他更简单的方法。