Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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# 根据使用ResX资源设置的图像查找PictureBox_C#_.net_Winforms_Picturebox_Embedded Resource - Fatal编程技术网

C# 根据使用ResX资源设置的图像查找PictureBox

C# 根据使用ResX资源设置的图像查找PictureBox,c#,.net,winforms,picturebox,embedded-resource,C#,.net,Winforms,Picturebox,Embedded Resource,我一直在检查图片盒是否有特定的图像。我已经使用Properties.Resources.TheImage设置了PictureBox的图像 使用下面的代码,在任何PictureBox控件中都找不到图像。我一直在努力做到这一点: foreach (Control X in Controls) { if (X is PictureBox) { if (((PictureBox)X).Image == Properties.Resources.TheImage)

我一直在检查
图片盒
是否有特定的图像。我已经使用
Properties.Resources.TheImage
设置了
PictureBox
的图像

使用下面的代码,在任何
PictureBox
控件中都找不到图像。我一直在努力做到这一点:

foreach (Control X in Controls)
{
    if (X is PictureBox)
    {
        if (((PictureBox)X).Image == Properties.Resources.TheImage)
        {
            MessageBox.Show("found the image");
        }
    }
}

Properties.Resources.XxxxYyy
属性每次使用时都会返回一个新位图。总的来说,内存使用量激增是一个令人讨厌的来源。您必须将其存储在表单构造函数中的变量中,现在可以对其进行比较

例如:

Bitmap _icopalABitmap = Properties.Resources.IcopalA;
Bitmap _icopalBBitmap = Properties.Resources.IcopalB;

然后检查特定的图像

Properties.Resources.SomeImage
每次使用时都返回不同的对象引用。您只需测试它:

var b = object.ReferenceEquals(Properties.Resources.SomeImage, 
                               Properties.Resources.SomeImage);
要检查图像的相等性,可以使用以下方法:

public bool AreImagesEqual(Image img1, Image img2)
{
    ImageConverter converter = new ImageConverter();
    byte[] bytes1 = (byte[])converter.ConvertTo(img1, typeof(byte[]));
    byte[] bytes2 = (byte[])converter.ConvertTo(img2, typeof(byte[]));
    return Enumerable.SequenceEqual(bytes1, bytes2);
}
例如:

var b = AreImagesEqual(Properties.Resources.SomeImage, 
                       Properties.Resources.SomeImage);

问题是什么?这篇文章似乎回答了你的问题。如果您对答案有任何疑问,或者您觉得它有用,请告诉我:)