EDIT October 2013: FastPix code modified to handle the Byte Array correctly. See post #38 below.
People are forever complaining about the slowness of GetPixel and SetPixel. They probably weren't designed for processing whole images (or if they were, they were designed very badly). DotNet has an answer to that, but it's not an easy one to get your head around: the Bitmap class methods LockBits and UnlockBits.
The main idea of FastPix is to provide a substitute for GetPixel and SetPixel which seem to work about 10 to 15 times as fast as the original ones. But it has more to offer.
There are plenty of examples on the web of classes that encapsulate LockBits/UnlockBits. The ones I have seen are often hampered because they try to deal with different bitmap formats. But as far as I can see nearly all the bitmaps we deal with are either 24 bits (digital photos, some drawing programs) or 32 bits (drawing programs with transparency). GDI+ converts 24 bit images internally to 32 bits anyway, so I have designed FastPix to work with 32 bits only. It makes the code simpler and opens up the possibility of processing bitmaps as Integer arrays, which are blindingly fast. FastPix includes a ConvertFormat method for converting bitmap pixel formats or you can use:
vb.net Code:
Dim img = Image.FromFile(filename)
Dim bmp As New Bitmap(img)
The bitmap New sub converts a loaded image (but not an image file) to 32 bits.
To start using Fastpix, download the attached zip file and unzip it to a convenient folder. In Visual Studio, select Project/Add Existing Item... and import the unzipped .vb file into your project.
Here is an example of how you use the FastPix GetPixel and SetPixel substitutes. Their format is the same as the ones in the Bitmap class:
vb.net Code:
Using fp as New FastPix(myBitmap)
Dim myColor As Color = fp.GetPixel(x, y)
fp.SetPixel(x, y, Color.Orange)
End Using
Always declare a FastPix object with a Using loop, or otherwise Dispose it as soon as you have finished with it. The UnLockBits is in the Dispose method, so you will not be able to see your resulting bitmap until Dispose has been called either implicitly (with End Using) or directly.
FastPix also offers you the bitmap in the form of an Integer Array and its performance leaves even the Fastpix GetPixel/SetPixel in the dust. Here's an example of how you use it:
vb.net Code:
Using fp as New FastPix(myBitmap)
'Make a local reference to the array; it is roughly 4x as fast as direct references to fp.PixelArray:
Dim pixels as Integer() = fp.PixelArray
For i as integer = 0 to pixels.Length - 1
'example: substitute a color
if pixels(i) = Color.Red.ToArgb then pixels(i) = Color.Blue.ToArgb
'example: invert the color
pixels(i) = pixels(i) XOR $HFFFFFF
Next
End Using
Note how the inversion is done using only a logical instruction and a bit mask. If you can restrict yourself to techniques like that and to integer arithmetic, and avoid all references to objects outside the Class, you can get fantastic performance. For example, inverting the colors of a 10 MP digital photo can be done over 100x as fast as with old-fashioned GetPixel and SetPixel (150x if you don't count the LockBits/UnlockBits overhead).
Of course, many kinds of pixel processing require you to get at the A, R, G and B bytes of the pixel. You can extract those from an integer using the BitConverter.GetBytes method, but that would slow things down terribly. Instead, it is possible to extract the bytes with masks and logic/shift operations only. They are probably just as fast as integer arithmetic. Here is an example which fades a bitmap by leveling all the Alpha byte values to a maximum level:
vb.net Code:
Dim _alpha As Byte 'Level Alpha down to this value
Using fp as New FastPix(myBitmap)
Dim pixels as Integer() = fp.PixelArray
Dim px As Integer = pixels(j)
Dim pxAlpha As Integer = (px >> 24) And &HFF 'shift alpha byte to bottom end, clear rest
If pxAlpha > _alpha Then
pxAlpha = CInt(_alpha) << 24
pixels(j) = (px And &HFFFFFF) Or pxAlpha 'replace original alpha value
End If
End Using
If you don't like the look of that, FastPix also provides a Byte array to make byte operations a bit less intimidating. Possibly it's not as fast as the integer array version. The bytes are in the order B, G, R, A for each pixel. Here's another example of fading a bitmap. Note the extra parameter in the FastPix declaration:
vb.net Code:
Using fp As New FastPix(myBitmap, True)
Dim bytes As byte() = fp.ColorByteArray
'Modify the Alpha bytes to make the bitmap 50% transparent:
For i As Integer = 3 to bytes.Length - 1 Step 4
bytes(i) = 127
Next
End Using
I hope you find it useful. Comments and criticism welcome. BB
Last edited by boops boops; Dec 9th, 2014 at 09:18 AM.
Reason: a few more typos spotted!