C#-使用Marshal.Copy将位图转换为byte []不能正常工作吗? [英] C# - Converting Bitmap to byte[] using Marshal.Copy not working consistently?

查看:69
本文介绍了C#-使用Marshal.Copy将位图转换为byte []不能正常工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试实现此处显示的图像比较算法:

我添加了一条注释,以显示此错误在方法中的何处发生.我认为这与内存分配不当有关,但是我无法弄清楚错误的根源是什么.

我可能还应该提到在将图像转换为字节数组时没有任何问题:

  ImageConverter转换器= new ImageConverter();byte [] b1bytes =(byte [])converter.ConvertTo(image1,typeof(byte [])); 

但是,这种方法要慢得多.

解决方案

如果(Width * bytesperpixel)!=步幅,则每行末尾将有未保证的未使用字节具有任何特定的值,并且在实践中可以填充随机垃圾.

您需要逐行迭代,每次都按Stride递增,并且仅检查实际上与每行像素相对应的字节.

I have been trying to implement the image comparing algorithm seen here: http://www.dotnetexamples.com/2012/07/fast-bitmap-comparison-c.html

The problem I have been having is that when I try to compare a large amount of images one after another using the method pasted below (a slightly modified version from the link above), my results seem to be inaccurate. In particular, if I try to compare too many different images, even the ones that are the same will occasionally be detected as different. The problem seems to be that certain bytes in the array are different, as you can see in the screenshot I have included of two of the same images being compared (this occurs when I repeatedly compare images from an array of about 100 images - but there are actually only 3 unique images in the array):

private bool byteCompare(Bitmap image1, Bitmap image2) {
        if (object.Equals(image1, image2))
            return true;
        if (image1 == null || image2 == null)
            return false;
        if (!image1.Size.Equals(image2.Size) || !image1.PixelFormat.Equals(image2.PixelFormat))
            return false;

        #region Optimized code for performance

        int bytes = image1.Width * image1.Height * (Image.GetPixelFormatSize(image1.PixelFormat) / 8);

        byte[] b1bytes = new byte[bytes];
        byte[] b2bytes = new byte[bytes];

        Rectangle rect = new Rectangle(0, 0, image1.Width - 1, image1.Height - 1);

        BitmapData bmd1 = image1.LockBits(rect, ImageLockMode.ReadOnly, image1.PixelFormat);
        BitmapData bmd2 = image2.LockBits(rect, ImageLockMode.ReadOnly, image2.PixelFormat);

        try
        {
            Marshal.Copy(bmd1.Scan0, b1bytes, 0, bytes);
            Marshal.Copy(bmd2.Scan0, b2bytes, 0, bytes);

            for (int n = 0; n < bytes; n++)
            {
                if (b1bytes[n] != b2bytes[n])  //This line is where error occurs
                    return false;
            }
        }
        finally
        {
            image1.UnlockBits(bmd1);
            image2.UnlockBits(bmd2);
        }

        #endregion

        return true;
    }

I've added a comment to show where in the method this error is occurring. I assume it has something to do with the memory not being allocated properly, but I haven't been able to figure out what the source of the error is.

I should probably also mention that I don't get any issues when I convert the image to a byte array like so:

ImageConverter converter = new ImageConverter();
byte[] b1bytes = (byte[])converter.ConvertTo(image1, typeof(byte[]));

However, this approach is far slower.

解决方案

If (Width * bytesperpixel) != Stride, then there will be unused bytes at the end of each line that are not guaranteed to have any particular value and in practice can be filled with random garbage.

You need to iterate line by line, increment by Stride each time, and only checking the bytes that actually correspond to pixels on each line.

这篇关于C#-使用Marshal.Copy将位图转换为byte []不能正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆