Beispiel #1
0
        public static bool CompareImages(PixelColor[,] p1, PixelColor[,] p2)
        {
            if (p1.GetLength(0) != p2.GetLength(0))
                return false;

            long red = 0, green = 0, blue = 0;
            for (int i = 1; i < p1.GetLength(0) - 1; i++)
            {
                for (int j = 1; j < p1.GetLength(1) - 1; j++)
                {
                    PixelColor color = p1[i, j];
                    PixelColor average = getAverageColor(p2, i, j);

                    PixelColor diff = color - average;
                    red += diff.Red;
                    green += diff.Green;
                    blue += diff.Blue;

                }
            }
            red = (int)((float)red / p1.Length);
            green = (int)((float)green / p1.Length);
            blue = (int)((float)blue / p1.Length);

            float total = (float)(red + green + blue) / 3f;

            return total < 6;
        }
 public static unsafe void CopyPixels(this BitmapSource source, PixelColor[,] pixels, int stride, int offset)
 {
     fixed(PixelColor* buffer = &pixels[0, 0])
       source.CopyPixels(
         new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight),
         (IntPtr)(buffer + offset),
         pixels.GetLength(0) * pixels.GetLength(1) * sizeof(PixelColor),
         stride);
 }
Beispiel #3
0
        public static PixelColor[,] CopyPixels(this BitmapSource source)
        {
            if (source.Format != PixelFormats.Bgra32)
            {
                source = new FormatConvertedBitmap(source, PixelFormats.Bgra32, null, 0);
            }
            PixelColor[,] pixels = new PixelColor[source.PixelWidth, source.PixelHeight];
            int      stride       = source.PixelWidth * ((source.Format.BitsPerPixel + 7) / 8);
            GCHandle pinnedPixels = GCHandle.Alloc(pixels, GCHandleType.Pinned);

            source.CopyPixels(
                new Int32Rect(0, 0, source.PixelWidth, source.PixelHeight),
                pinnedPixels.AddrOfPinnedObject(),
                pixels.GetLength(0) * pixels.GetLength(1) * 4,
                stride);
            pinnedPixels.Free();
            return(pixels);
        }