【问题标题】:How to check if image object is the same as one from resource?如何检查图像对象是否与资源中的对象相同?
【发布时间】:2015-03-01 05:17:47
【问题描述】:

所以我正在尝试创建一个简单的程序,当点击它时,它只改变图片框中的图片。我现在只使用两张图片,所以我的图片框点击事件函数的代码 看起来像这样:

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's answer 建议的修复一起工作,但添加 else 并没有解决问题 - 逐步执行带有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 总是被评估;所以它将撤消第一个 if 语句所做的事情。应该将其移至 else 子句。也许还需要调用一个刷新函数。
  • 我已经对问题进行了重大更新 - 请检查这是否符合您的意图。请注意,我尝试将问题与 Hans Passant 的答案相匹配,因为它包含实际解释。

标签: c# embedded-resource


【解决方案1】:
     if (pictureBox1.Image == Labirint.Properties.Resources.first)

这里有一个陷阱,没有足够多的 .NET 程序员意识到。负责运行大量内存占用的很多程序。使用 Labirint.Properties.Resources.xxxx 属性创建一个 new 图像对象,它永远不会匹配任何其他图像。您只需使用该属性一次,将图像存储在类的字段中。大致:

    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();
    }

【讨论】:

  • 关于资源图像的好答案。我已尝试更新问题,使其与您的答案相匹配 - 看看是否可以进一步改进问题以使其更容易找到。
  • VB 也一样,但比较的是:如果 pictureBox1.Image IS first Then ...
【解决方案2】:

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

这是要求:

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");
        }

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

【讨论】:

  • 我真诚地怀疑他是否需要对图像进行字节比较。他做作业和检查的方式,一个简单的参考比较就足够了。
  • 是的,我也觉得没必要,我一直用这种方式,但也有点慢
  • 我想大图像会非常
【解决方案3】:

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

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 语句。如果我只输入“pictureBox1.Image = Labirint.Properties.Resources.first;”它会更改框中的图像(“reitmi”是初始图片),但是如果我添加一个 if 语句(即使是单个 if)它也不起作用
  • pictureBox1.Image = 行上放一个断点,看看它们是否被命中。
  • 不,他们不是,“pictureBox1.Image”的值是“System.Drawing.Image”,根据调试器,为什么不是第一个reitmi?
  • 那不是值,那是类型。你能用你的旧代码试试看它是否等于它们中的任何一个吗?
  • 两列的值和类型都是一样的——“System.Drawing.Image”
猜你喜欢
  • 1970-01-01
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
  • 1970-01-01
相关资源
最近更新 更多