Calculating the required buffer size for the WriteableBitmap.WritePixels method

前端 未结 6 2203
甜味超标
甜味超标 2020-12-18 20:19

How do I calculate the required buffer size for the WriteableBitmap.WritePixels method?

I am using the overload taking four parameters, the first is an Int32Rect, th

6条回答
  •  独厮守ぢ
    2020-12-18 20:32

    The stride value is calculated as the number of bytes per "pixel line" in the write rectangle:

    var stride = (rect.Width * bitmap.Format.BitsPerPixel + 7) / 8;
    

    The required buffer size is the number of bytes per line multiplied by the number of lines:

    var bufferSize = rect.Height * stride;
    

    Provided that you have a 2x2 write rectangle and a 32-bits-per-pixel format, e.g. PixelFormats.Pbgra32, you get stride as 8 and bufferSize as 16.

提交回复
热议问题