I'm working on an application where the performance is very important. This application requires lots of image processing so, as most of us know that Bitmap's pixels accessing using GDI+ methods GetPixel
and SetPixel
is quite slow. To solve this issue we use Bitmap.LockBits
and Bitmap.UnlockBits
methods and i'm totally aware of how to access pixels using this method but my question is:
What is the performance of both Bitmap.LockBits
and Bitmap.UnlockBits
? Do they perform any pixels copying or something that may have non-linear order?
I'm asking this question because I found a lots of calling for Bitmap.LockBits
and Bitmap.UnlockBits
methods in my code. I made a search but I didn't find anything
Asked
Active
Viewed 1,033 times
0

Ibrahim Amer
- 1,147
- 4
- 19
- 46
-
Parhaps this [thread](http://stackoverflow.com/questions/7535812/c-sharp-lockbits-perfomance-int-to-byte) on Bitmap performance might help you. My understanding is that LockBits creates a **copy** of the Bitmap in managed memory which takes **O(n)** time. By using *unsafe* code you can circumvent that. – MrPaulch Apr 30 '15 at 07:41
-
2To find out where your program spends time, you need to measure it. Nothing else will tell you whether you have a performance problem in a specific area or not. – Lasse V. Karlsen Apr 30 '15 at 07:49
1 Answers
0
LockBits method returns BitmapData object, which is used to describe the memory sector.
BitmapData _bmd = _bmp.LockBits(new Rectangle(0, 0, _bmp.Width,_bmp.Height) , ImageLockMode ReadWrite, _bmp.PixelFormat);
Take a look here http://www.mfranc.com/programming/operacje-na-bitmapkach-net-1/
Update:
Lockbits will copy the bitmap data from a bitmap to a location in memory that is ready to be read/written to. The lockbits function will give you Scan0 that is a pointer to the start of this bitmap data.So it involves copying but even that would be much faster when compared to the Normal GDI+ Operation as per the comparison chart above.Also look out for out of bound memory .

techno
- 6,100
- 16
- 86
- 192
-
1This does not answer the question. The question is not whether accessing pixels this way is faster, it is whether calling `LockBits` many times will have negative impact on performance. This may dictate a different solution, like only calling LockBits once and passing the direct access data structures around instead of just passing the bitmap around. – Lasse V. Karlsen Apr 30 '15 at 07:51
-
Thank you sir for your answer, but as @Lasse V.Karlsen mentioned my question is not about the performance difference between GDI+ and LockBits, UnlockBits methods, it is about the performance impact when calling them many times – Ibrahim Amer Apr 30 '15 at 08:06
-
@LasseV.Karlsen thank you sir for your reply, if you allow me to ask a question, if I have a Bitmap with its region is already locked, is there any method to get access to BitmapData Object without calling LockBits function – Ibrahim Amer Apr 30 '15 at 08:10
-
1Not that I know of, but my comment on your question still stands. Don't start with changing code (unless it is because you need to add performance counters or timing, but you should ideally use a proper performance measuring tool), start with measuring it. – Lasse V. Karlsen Apr 30 '15 at 08:17