I have a “performance critical” operation where I need to work with 1bpp images.
Actually I’m using the Bitmap class and I’m doing, each iteration of a graphic update cycle, a copy of the bitmap inside the byte array.
Watching my task manager, this is not that I can keep doing: it uses 2% cpu all time, I think it’s quite a lot for something like an utility program.
I need to waste less memory as possible and almost 0 cpu. The image is 160×43, quite small.
Why I am not using directly the byte array? Easy: I would like to write over it, do some common operations which I don’t want to rewrite by myself.
I can use obviusly a different image class (from wpf for example, I don’t know). I need the possibility to work with a 1bpp image.
Offtopic:
I have the same “problem” with a 32bpp image, I need a way to work with it as an image while it is a byte array, I can’t make a copy of my bytes each time!!! I’m wasting cpu in this way.
You should use
Bitmap.LockBitsto get the underlying memory of the bitmap pinned (so the friendly garbage collector won’t move it around for you).As an example, this small program loads a png, sets a simple pixel pattern, and saves the resulting image:
LockBits will have an overhead depending on whether it needs to convert the internal memory representation to what you request (so it might matter where you get it from). As alwyas with performance, measure and profile to find your bottlenecks.