2

I'm having multiple threads that need to READ the same bitmap. They all need to potentially read the entire bitmap.

I want them to access the underlying bytes, so each thread invokes LockBits with ReadOnly lockmode.

BitmapData bits = videoEntry.Data.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);

var bytes = new byte[width * height * 4];
Marshal.Copy(bits.Scan0, bytes, 0, bytes.Length);

videoEntry.Data.UnlockBits(bits);

DoSomethingThreadSpecificWithByteArray(bytes, 0, bytes.Length);

Unfortunately, two consecutive calls to LockBits throws an exception System.InvalidOperationException: 'Bitmap region is already locked.' Which kind of makes sense, although I only want to read the memory.

My questions:

Is there another/better way to read the bytes of a Bitmap from multiple threads?

Obviously, I can preprocess the byte array and let all threads access that, but I guess it's nice to keep the Bitmap classes together.

Ben
  • 1,519
  • 23
  • 39

0 Answers0