Calculating the required buffer size for the WriteableBitmap.WritePixels method

前端 未结 6 2211
甜味超标
甜味超标 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:45

    The stride is simply the width in bytes of your input buffer. It is called stride, because sometimes there is extra memory behind each line of an image, which makes it impossible to use the width of the image to read each line of an image.

    So in your example, this is 2. You do not need to calculate anything with the bits per pixel of the bitmap, the WritePixels method knows all this information. You need to provide the information about how your input data is structured.

    However, as mentioned in the other answer, your example won't work if the bitmap is also 2x2. Then the starting coordinate would be 0,0.

    EDIT:

    When I look closer at your example, I see the mistake. You say the colourData is the input color. But this is input per pixel. So if you want to change a rect of 2x2, you need the following inputdata:

    byte[] colourData = { 0, 0, 0, 0, 0, 0, 0, 0,
                          0, 0, 0, 0, 0, 0, 0, 0 };
    

    And then the bytes per pixel is equal to that of the bitmap, so that is 4, times the width of each line (2), makes total 8.

提交回复
热议问题