c# - Compare two image data using unsafe method -
i writing function difference between 2 bitmap images in visual studio 2010. have function takes 2 bitmap images parameters, use unlock bits data of each pixel,both images of equal resolution , dimensions.
when use unlock bits 1 image works well, when use both simultaneously in same function gives exception
bitmap region locked
code:
public bitmap invert(bitmap b,bitmap c) { bitmapdata bmdata = b.lockbits(new system.drawing.rectangle(0, 0, b.width, b.height), imagelockmode.readwrite, system.drawing.imaging.pixelformat.format24bpprgb); int stride = bmdata.stride; system.intptr scan0 = bmdata.scan0; // image 2 bitmapdata data2 = c.lockbits(new system.drawing.rectangle(0, 0, c.width, c.height), imagelockmode.readwrite, system.drawing.imaging.pixelformat.format24bpprgb); int stride1 = data2.stride; system.intptr scan1 = data2.scan0; unsafe { byte* p = (byte*)(void*)scan0; byte* q = (byte*)(void*)scan1; noffset = stride - b.width * 3; nwidth = b.width * 3; (y = 0; y < b.height; ++y) { (x = 0; x < nwidth; ++x) { p[0] = (byte)(p[0]-q[0]); ++p; ++q; } p += noffset; q += noffset; } } b.unlockbits(bmdata); c.unlockbits(data2); return b; }
becuase need compare images , suggesst opening them in read mode i.e:
bitmapdata data2 = c.lockbits(new system.drawing.rectangle(0, 0, c.width, c.height), imagelockmode.readonly, system.drawing.imaging.pixelformat.format24bpprgb);
Comments
Post a Comment