Does Bitmap.LockBits “Fix” a Bitmap in Memory?

Recently, I have been using locked bitmaps, and I keep getting "trying to access the wrong memory" errors. This is mainly due to the fact that the bitmap is moved in memory. Some people use GCHandle.Alloc()to allocate memory in the CLR and to connect it. Does it Bitmap.LockBits()do the same? I don’t understand the difference between “locking” the memory and “fixing” the memory. Can you also explain the terminology and differences, if any?

+5
source share
2 answers

GCHandle.Alloc- a more general method that allows you to assign a handle to any managed object and bind it in memory (or not). Attaching memory prevents the GC from moving it, which is especially useful when you need to pass some data, such as an array, into unmanaged code.

GCHandle.Alloc won't help you access the bitmap data in any way, because binding to this object will simply prevent the managed object from moving (Bitmap object) (and garbage collection).

, , GDI + BITMAP. - , , GDI + bitmap. - Bitmap.LockBits - , , GdipBitmapLockBits. , , GDI +, , GC.

, LockBits, BitmapData.Scan0 - . , BitmapData.Scan0 + Height * Stride.

UnlockBits, .

+8

attempted to access invalid memory, , , , , , .

, 85000 , 85K .

, , , ++ . , . GCHandle.Alloc (imageArray,GCHandleType.Pinned);, Free, .

+1

All Articles