Lock your bits - Faster C# bitmap manipulation
Windows Mobile Team Blog - Faster C#
"...
Today’s lesson is fast bitmap manipulation. Trying to alter pixels on an image is an intensive operation due to the problems associated with accessing managed memory. It’s just plain slow. This won’t be a big concern if all you are doing is drawing an image to a graphics object – but when you want to run a per-pixel filter on an image you quickly hit a performance bottleneck.
For this tutorial lets create a few classes that will let us invert the pixels on an image. The first thing we need to do is create a faster bitmap class. I’m going to make a class called FastBitmap, but I can’t subclass from Bitmap since it’s sealed. Oh no! Relax … we can fix this. Let’s create a class that contains a member variable that is a bitmap.
...
But we still haven’t solved our performance problems. How do we quickly manipulate the pixels on an image? Simple – lock the pixels in memory so we can access them quickly and in a contiguous fashion. This is done via the LockBits method. I’m going to add the following methods to our FastBitmap class that allow us to put the managed pixel data into a format that is better suited for direct manipulation.
...
Now we have an algorithm that in union with our FastBitmap class can invert a 320x240 image nearly instantly on a mobile device.
..."
Interesting...
It's been a bit since I've had to play with images, but I will be returning to Managed Image Land one day in the not to distant future, so wanted capture this tip.
Related Past Post XRef:
Converting a Color Bitmap to Greyscale with .Net (referenced article doesn't exist anymore, but you can find it in the Google Cache)
Playing with Text and GDI+ in VB.Net - System.Drawing.Fun
Bob Powell's GDI+ FAQ
No comments:
Post a Comment