private double computeError(Bitmap sourceImage) { double error = 0; BitmapData bd2 = OriginalImage.LockBits( new Rectangle(0, 0, OriginalImage.Width, OriginalImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); Pixel[] sourcePixels = null; BitmapData bd = sourceImage.LockBits( new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); sourcePixels = new Pixel[sourceImage.Width * sourceImage.Height]; unsafe { fixed(Pixel *psourcePixels = sourcePixels) { Pixel *pSrc = (Pixel *)bd.Scan0.ToPointer(); Pixel *pDst = psourcePixels; for (int i = sourcePixels.Length; i > 0; i--) { *(pDst++) = *(pSrc++); } } } sourceImage.UnlockBits(bd); unchecked { unsafe { fixed(Pixel *psourcePixels = sourcePixels) { Pixel *p1 = (Pixel *)bd2.Scan0.ToPointer(); Pixel *p2 = psourcePixels; for (int i = sourcePixels.Length; i > 0; i--, p1++, p2++) { int r = p1->R - p2->R; int g = p1->G - p2->G; int b = p1->B - p2->B; error += r * r + g * g + b * b; } } } } OriginalImage.UnlockBits(bd); return(Math.Sqrt(error)); }