SetPixel() for accessing individual pixels in the bitmap - It is too slow and therefore it not suitable to image processing. The following FastBitmap class allows us to fastly access the raw memory of bitmap data via pointers.
FastBitmap implement the
IDisposable Interface. The constructor takes the bitmap we want to process and an optional rectangle of the bitmap we want to access. It will locks the raw memory of bitmap and will allow us to access the raw memory. The
Dispose method should be called to unlock the memory and free the unmanaged resources when we done with processing the bitmap.
The class provides some properties which allows to locate individual pixels in the bitmap
PixelSizeholds the size of pixel in bytes
Strideholds the width in bytes of row of pixels of the image
Scan0holds a pointer to the location of the left top pixel of the rectangle we want to process.
YYare the left and top of the rectangle of the bitmap we want to process.
Heightare the width and height of the rectangle of the bitmap we want to process.
fb = new FastBitmap(bitmap,3,1,4,3); ... fb.Dispose()
Suppose the bitmap is color image in 24bpp-BGR:
fb.PixelSizeis 3 ( Each pixel has 3 bytes)
bitmap's width= 3 * 9 = 27
fb.scan0will point to pixel located at
Using those values we can access the pixel (xx,yy) in the rectangle by the following calculation:
fb.Scan0+ yy *
fb.Stride+ xx *