Why does BitmapData.Stride ignore the size of the rectangle passed to LockBits?

StackOverflow https://stackoverflow.com/questions/10032104

  •  29-05-2021
  •  | 
  •  

سؤال

I have an image and want to read the pixel values in a particular region.

I assumed that the rectangle which is the 1st argument of the LockBits() method was there to allow for sub-sections to be specified. When debugging though, I noticed that the value of BitmapData.Stride was 3 x the width of the original bitmap, not 3 x the width of the rectangle.

Why is this? Have I misunderstood the purpose of the rectangle parameter?

هل كانت مفيدة؟

المحلول

It gives GDI+ a way to optimize the mapping of the pixel data to memory. Using a smaller rectangle requires fewer memory pages. The stride is the same, the stride of the original bitmap. And you have to use that value when indexing the scan lines in the bitmap, regardless of the size of the rectangle.

نصائح أخرى

It does that so it can provide you a pointer into the buffer it already allocated, instead of allocating a new one and copying the pixel data to it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top