private void CompareInternal(Bitmap one, Bitmap two) { if (one.Width != two.Width || one.Height != two.Height) { //Sizes do not match return; } var lockedOne = new LockBitmap(one); var lockedTwo = new LockBitmap(two); var numberOfDifferentPixels = 0; for (int i = 0; i < one.Width; i++) { for (int j = 0; j < one.Height; j++) { if (lockedOne.GetPixel(i, j) != lockedTwo.GetPixel(i, j)) { numberOfDifferentPixels++; } } } Debug.WriteLine("Different pixels: " + numberOfDifferentPixels); }
public static LockBitmap GetLockBitmap(int x, int y, int w, int h) { LockBitmap lockb = null; Rectangle bounds = new Rectangle(x, y, w, h); Bitmap bmp = new Bitmap(w, h); int maxTries = 3; int nbTries = 0; while (lockb == null && ++nbTries <= maxTries) { try { using (Graphics g = Graphics.FromImage(bmp)) g.CopyFromScreen(bounds.Location, System.Drawing.Point.Empty, bounds.Size); lockb = new LockBitmap(bmp); } catch (Exception ex) { Debug.Log("Failed to take screenshot: " + ex.Message); } } if (lockb == null) { throw new Exception("Failed to create bitmap"); } return(lockb); }
public static List <Color> GetColors(LockBitmap lockb, PointGroup pg) { List <Color> colors = new List <Color>(); if (!lockb.Locked) { throw new InvalidOperationException("Bits must be locked to get colors."); } foreach (var point in pg.Points) { if (point.X < 0 || point.X >= lockb.Width) { throw new InvalidOperationException($"Invalid point x {point.X} must be between 0 and {lockb.Width}"); } else if (point.Y < 0 || point.Y >= lockb.Width) { throw new InvalidOperationException($"Invalid point x {point.Y} must be between 0 and {lockb.Height}"); } Color col = lockb.GetPixel(point.X, point.Y); colors.Add(col); } return(colors); }
private void InitBitmapIfNull() { if (lockB == null) { lockB = ImgUtils.GetLockBitmap(rect); lockB.LockBits(); } }
public void Dispose() { if (lockB != null) { lockB.UnlockBits(); lockB.Dispose(); } lockB = null; }
public void RenderImage(Bitmap bitmap, ImageView imageView) { var lockedBitmap = new LockBitmap(bitmap); lockedBitmap.LockBits(); var width = imageView.Width; for (int i = 0; i < imageView.Height; i++) for (int j = 0; j < imageView.Width; j++) { if (imageView.Pixels[i * width + j] != Color.FromArgb(0, 0, 0, 0)) { lockedBitmap.SetPixel(j, i, imageView.Pixels[i * width + j]); } } lockedBitmap.UnlockBits(); }
private void CompareInternal(Bitmap one, Bitmap two) { if (one.Width != two.Width || one.Height != two.Height) { //Sizes do not match return; } var lockedOne = new LockBitmap(one); var lockedTwo = new LockBitmap(two); var numberOfDifferentPixels = 0; for (int i = 0; i < one.Width; i++) for (int j = 0; j < one.Height; j++) { if (lockedOne.GetPixel(i, j) != lockedTwo.GetPixel(i, j)) { numberOfDifferentPixels++; } } Debug.WriteLine("Different pixels: " + numberOfDifferentPixels); }