C#参考比较

发布于 2024-10-27 06:36:16 字数 1278 浏览 6 评论 0原文

有人知道当您尝试比较两个 System.Drawing.Image 实体时可能会出现什么问题吗?

我有一些 IEnumerable; images,它是使用 Image.FromFile(path) 方法迭代构造的。

但是下面的代码产生了我不太理解的结果:

foreach (var image1 in images)
{
    foreach (var image2 in images)
    {
        if (image1 == image2)
        {
            // (Do something!)
        }
    }
}

问题是 (Do Something!) 部分永远不会被调用。

调试器显示图像对象有一个名为 nativeImage 的属性,我认为它是原始内存指针,因为 System.Drawing.Image 是使用编组实现的。

这个内存指针一直在改变,我猜这里发生了某种克隆,但我不明白我实际上应该做什么。

我做错了什么?如何实际比较从一个 IEnumerable 序列中获取的 System.Drawing.Image 对象以查看它们是否相同?谢谢


更新

        var paths = new List<String> {"Tests/1_1.jpg", "Tests/1_2.jpg"};
        IEnumerable<Image> images = paths.Select(path => Image.FromFile(path)).ToList();

        foreach (var image1 in images)
        {
            foreach (var image2 in images)
            {
                if (ReferenceEquals(image1, image2))
                {
                    
                }
            }
        }

如果没有ToList(),这显然不起作用,我很愚蠢。

谢谢大家。

Does someone know what might go wrong, when you attempt to compare two System.Drawing.Image entities?

I have some IEnumerable<Image> images, which is constructed iteratively using Image.FromFile(path) method.

But the following code yields the result I can't quite understand:

foreach (var image1 in images)
{
    foreach (var image2 in images)
    {
        if (image1 == image2)
        {
            // (Do something!)
        }
    }
}

The thing is that the (Do something!) part never gets called.

Debugger shows that the image objects have a property called nativeImage, which, as I assume is a raw memory pointer, because System.Drawing.Image is implemented using marshalling.

This memory pointer is changed all the time and I guess some sort of cloning happens here, but I can't understand what should I actually do.

What am I doing wrong and how can I actually compare System.Drawing.Image objects taken from one IEnumerable<> sequence to see if they are the same?

Thank you.


Update

        var paths = new List<String> {"Tests/1_1.jpg", "Tests/1_2.jpg"};
        IEnumerable<Image> images = paths.Select(path => Image.FromFile(path)).ToList();

        foreach (var image1 in images)
        {
            foreach (var image2 in images)
            {
                if (ReferenceEquals(image1, image2))
                {
                    
                }
            }
        }

Without the ToList(), this obviously didn't work, I'm very stupid.

Thanks everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

oО清风挽发oО 2024-11-03 06:36:16

请记住,每次调用 GetEnumerator 时,您都会看到每次调用 MoveNext 时返回的新对象。您需要做的是将迭代强制放入列表中。

var imageList = images.ToList();

Remember that each time you call GetEnumerator you will see new objects returned for each call to MoveNext. What you need to do is force the iteration into a list.

var imageList = images.ToList();
夜深人未静 2024-11-03 06:36:16

通过image1 == image2,您只是比较图像的引用(而不是逐像素地比较图像)。

通过调用 Image.FromFile(path) ,您每次调用此方法时都会创建新的图像对象(即使路径相同),因此它们始终具有不同的引用。

我不知道是否有一种方法可以逐像素比较图像,但是当然您可以为其实现自己的机制(看起来并不难)。

By image1 == image2 you're only comparing the references of the image (not image as pixel by pixel).

By invoking Image.FromFile(path) you create new image object every time you call this method (even if the path is the same) so they always have different references.

I don't know if there is a method to compare images pixel by pixel, but of course you can implement your own mechanism for it (it doesn't look difficult).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文