<summary>///image comparison. Used to find the difference between two images////</summary> public class Imagecomparer {//<summary>///Image Color///</summary> [StructLayout (layou TKIND.EXPLICIT)] Private struct Iccolor {[FieldOffset (0)] public byte B [FieldOffset (1)] public byte G; [FieldOffset (2)] public byte R; }///<summary>///by 20*20 size to compare two images. </summary>//<param name= "BMP1" ></param>//<param name= "BMP2" ></p aram>//<returns></returns> public static list<rectangle> Compare (Bitmap bmp1 , Bitmap bmp2) {return Compare (BMP1, BMP2, New Size (20, 20)); }///<summary>///Compare two images///≪/summary>//<param name= "BMP1" ></param>//<param name= "BMP2" ></PARAM&G T <param name= "block" ></param>///<returns></returns> public static list& Lt Rectangle> Compare (Bitmap bmp1, Bitmap bmp2, Size block) {list<rectangle> rects = new List<rectangle> (); PixelFormat PF = Pixelformat.format24bpprgb; BitmapData BD1 = bmp1. LockBits (New Rectangle (0, 0, Bmp1. Width, Bmp1. Height), imagelockmode.readonly, PF); BitmapData Bd2 = bmp2. LockBits (New Rectangle (0, 0, bmp2. Width, BMP2. Height), imagelockmode.readonly, PF); try {unsafe {int w = 0, h = 0; while (H < bd1. Height && H < Bd2. Height) {byte* P1 = (byte*) bd1. Scan0 + H * BD1.Stride; byte* P2 = (byte*) bd2. Scan0 + H * bd2. Stride; w = 0; while (W < BD1. Width && W < Bd2. Width) {//scan by block size for (int i = 0; I < block. Width; i++) {int WI = w + i; if (WI >= bd1. Width | | WI >= bd2. Width) break; for (int j = 0; J < block. Height; J + +) {int HJ = h + j; if (HJ >= bd1. Height | | HJ >= Bd2. Height) break; iccolor* PC1 = (iccolor*) (P1 + wi * 3 + BD1. Stride * j); iccolor* PC2 = (iccolor*) (P2 + wi * 3 + bd2. Stride * j); if (Math.Abs (pc1->r-pc2->r) >10 | | pc1->g! = PC2->G | | pc1->b! = pc2->b) if (Math.Abs (pc1->r-pc2->r) > 100 | | Math.Abs (pc1->g-pc2->g) > 100 | | Math.Abs (pc1->b-pc2->b) > 100) { The current block has a pixel point with a different color value. int bw = Math.min (block. Width, Bd1. WIDTH-W); int BH = math.min (block. Height, Bd1. HEIGHT-H); Rects. ADD (New Rectangle (W, h, BW, BH)); Goto E; }}} E: W + = block. Width; } H + = block. Height; } }} finally {Bmp1. Unlockbits (BD1); Bmp2. Unlockbits (BD2); } return rects; } }
. NET Picture Pixel comparison