code_junkie

How to manipulate images at pixel level in C#?

Nov 11th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. public static Bitmap Diff(Bitmap src1, Bitmap src2, int x1, int y1, int x2, int y2, int width, int height)
  2. {
  3. Bitmap diffBM = new Bitmap(width, height, PixelFormat.Format24bppRgb);
  4.  
  5. for (int y = 0; y < height; y++)
  6. {
  7. for (int x = 0; x < width; x++)
  8. {
  9. //Get Both Colours at the pixel point
  10. Color col1 = src1.GetPixel(x1 + x, y1 + y);
  11. Color col2 = src2.GetPixel(x2 + x, y2 + y);
  12.  
  13. //Get the difference RGB
  14. int r = 0, g = 0, b = 0;
  15. r = Math.Abs(col1.R - col2.R);
  16. g = Math.Abs(col1.G - col2.G);
  17. b = Math.Abs(col1.B - col2.B);
  18.  
  19. //Invert the difference average
  20. int dif = 255 - ((r+g+b) / 3);
  21.  
  22. //Create new grayscale rgb colour
  23. Color newcol = Color.FromArgb(dif, dif, dif);
  24.  
  25. diffBM.SetPixel(x, y, newcol);
  26.  
  27. }
  28. }
  29.  
  30. return diffBM;
  31.  
  32. }
Add Comment
Please, Sign In to add comment