質問

I really hope someone can point me in the right direction as I'm pulling the last bit of my hair out!

What I'm Doing: I'm working on an image compare project where I download an Image from a url and comparing it to an image stored previously on my drive. If the url image is not the same as the one on file, a new file is created (then that image is used for comparing with the latest image downloaded from the url).

  • The image downloaded is using MemoryStream and Bitmap.FromStream(ms) to create the image (using WebClient DownloadData) - works perfectly
  • The image is being stored on file by converting it to a byte array and using File.WriteAllBytes - works perfectly

So I'm successfully able to download, save and read an image.

Here's my issue: The bytes from the image downloaded is more than the bytes from the original image stored on file which is rendering my Image Compare method useless.

Both images are exactly the same, and are visually the same. The Resolution, Format, Pixel Format all identical, but the bytes don't match and I'm at a loss as to why?

byte[] byteNew.Length = {byte[28468]} //(From Url)
byte[] byteOld.Length = {byte[28335]} //(From File - but file length in notepad is 28468)

Is there something I'm missing?

Any suggestions would be greatly appreciated! But please, no 3rd party tool suggestions

役に立ちましたか?

解決

You are saving the downloaded image from a bitmap, it means you are reencoding it, because of that the images are different.

If you want they to be equal, then save the original array without processing.

Also, to compare image bytes already encoded is not a good idea if what you want is to compare the pixel data and not the encoded data (a png and a bitmap can represent exactly the same image but the encoded array will be completely different)

If you want to compare pixel data, then you can load both bitmaps, use LockBits and then compare the pixel data.

他のヒント

If anyone's interested in comparing two bitmaps, here's what I found from Gussman's comment on using LockBits.

Below is a compacted version of MSDN's example for returning pixel data from a bitmap... which can then be used for comparison or image manipulation.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace Example
{
    public class ImageData
    {
        public static byte[] BytesFromImage(Image image)
        {
            //Parse the image to a bitmap
            Bitmap bmp = new Bitmap(image);

            // Set the area we're interested in and retrieve the bitmap data
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData = bmp.LockBits(rect, Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);            

            // Create a byte array from the bitmap data
            int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
            byte[] rgbValues = new byte[bytes];
            IntPtr ptr = bmpData.Scan0;
            Marshal.Copy(ptr, rgbValues, 0, bytes);

            bmp.UnlockBits(bmpData);

            //return the byte array
            return rgbValues;
        }
    }
}

more information can be found here: http://msdn.microsoft.com/en-us/library/5ey6h79d(v=vs.110).aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top