private static Statistics RenderAndDiff(ImageGraphic control, ImageGraphic test, string controlName, string testName) { List <int> diffs = new List <int>(); using (Bitmap controlBitmap = new Bitmap(control.Columns, control.Rows)) { using (Bitmap testBitmap = Render(test)) { for (int x = 0; x < control.Columns; x++) { for (int y = 0; y < control.Rows; y++) { if (control is ColorImageGraphic) { Color controlColor = Color.FromArgb(control.PixelData.GetPixel(x, y)); Color testColor = testBitmap.GetPixel(x, y); diffs.Add(Math.Abs(controlColor.R - testColor.R)); diffs.Add(Math.Abs(controlColor.G - testColor.G)); diffs.Add(Math.Abs(controlColor.B - testColor.B)); controlBitmap.SetPixel(x, y, controlColor); } else if (control is GrayscaleImageGraphic) { int range = 1 << ((GrayscaleImageGraphic)control).BitsStored; int offset = ((GrayscaleImageGraphic)control).IsSigned ? -(range >> 1) : 0; int v = (int)(256f * (control.PixelData.GetPixel(x, y) - offset) / range); Color controlColor = Color.FromArgb(255, v, v, v); Color testColor = testBitmap.GetPixel(x, y); diffs.Add(Math.Abs(controlColor.R - testColor.R)); diffs.Add(Math.Abs(controlColor.G - testColor.G)); diffs.Add(Math.Abs(controlColor.B - testColor.B)); controlBitmap.SetPixel(x, y, controlColor); } else { Assert.Ignore("Unable to render and diff images of type {0}", control.GetType().FullName); } } } if (!string.IsNullOrEmpty(testName)) { testBitmap.Save(testName); } } if (!string.IsNullOrEmpty(controlName)) { controlBitmap.Save(controlName); } } return(new Statistics(diffs)); }