Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查图像对象是否与资源中的图像对象相同?_C#_Embedded Resource - Fatal编程技术网

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

C# 如何检查图像对象是否与资源中的图像对象相同?,c#,embedded-resource,C#,Embedded Resource,因此,我试图创建一个简单的程序,当点击图片框时,只需更改图片。我现在只使用了两张图片,所以我的图片框代码点击事件函数 看起来是这样的: private void pictureBox1_Click(object sender, EventArgs e) { if (pictureBox1.Image == Labirint.Properties.Resources.first) pictureBox1.Image = Labirint.Propert

因此,我试图创建一个简单的程序,当点击图片框时,只需更改图片。我现在只使用了两张图片,所以我的图片框代码点击事件函数 看起来是这样的:

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撤销第一个if条件的效果将与建议的修复一起工作,但是添加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; 
如果需要使用else,因为如果图像是第一个,则将其设置为reitmi,然后检查它是否是reitmi,现在是,并将其更改回第一个。这最终看起来根本没有改变

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");
        }
此代码比较每个字节图像以生成结果。可能还有其他更简单的方法

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

谢谢,但这并没有真正帮助我,因为它似乎没有进入if语句。如果我只是键入pictureBox1.Image=Labirint.Properties.Resources.first;它会改变框中的图像,reitmi是初始图片,但是如果我添加一个if语句,即使它不工作,也要在pictureBox1.image=行上放置一个断点,看看它们是否被命中。不,它们不是,pictureBox1.image值是System.Drawing.image,根据调试器,为什么它不是第一个?那不是值,那是类型。你能试着用你的旧代码看看它是否等于它们中的任何一个吗?这两列的值和类型都是一样的-System.Drawing.image我真的怀疑他是否需要对图像进行字节比较。他做作业和检查的方式,一个简单的参考比较就足够了。是的,我也认为没有必要,我总是用这种方式,但它有点慢,我想它对于大图像来说会非常慢。第二个if总是被评估;因此,它将撤销第一个if语句所做的操作。应将其移至else子句。也许还需要调用刷新函数。我已经显著更新了问题-请检查这是否符合您的意图。请注意,我试图将这个问题和Hans Passant的答案匹配起来,因为它包含了实际的解释。关于参考资料中的图像的回答很好。我尝试更新问题,使其与您的答案相匹配-查看问题是否可以进一步改进以使其更易于查找。VB也是如此,但比较结果是:如果pictureBox1.Image是第一个,则。。。