none
stride parameter in BitmapSource.Create RRS feed

  • Question

  • The last parameter in the BitmapSource.Create() method is stride.

    Here is the code snippet in the MSDN.

    int width = 128;
    int height = width;
    int stride = width/8;
    byte[] pixels = new byte[height*stride];
    .
    .
    .
    .
    BitmapSource image = BitmapSource.Create(
        width,
        height,
        96,
        96,
        PixelFormats.Indexed1,
        myPalette, 
        pixels, 
        stride);

    And here is a another code snippet.

    PixelFormat pf = PixelFormats.Bgr32;
    int width = 200;
    int height = 200;
    int rawStride = (width * pf.BitsPerPixel + 7) / 8;
    byte[] rawImage = new byte[rawStride * height];
    
    // Initialize the image with data.
    Random value = new Random();
    value.NextBytes(rawImage);
    
    // Create a BitmapSource.
    BitmapSource bitmap = BitmapSource.Create(width, height,
        96, 96, pf, null,
        rawImage, rawStride);

    I don't understand what stride means and why stride = width / 8 and rawStride = (width * pf.BitsPerPixel + 7) / 8.

    Can anybody explain about this?



    • Edited by Jeff0803 Tuesday, March 14, 2017 5:27 PM
    • Moved by CoolDadTx Tuesday, March 14, 2017 5:31 PM WPF related
    Tuesday, March 14, 2017 4:51 PM

Answers

  • >>Can anybody explain about this?

    Stride is pixel size in bytes. Please refer to the answer and the comments in the following thread for more information: http://stackoverflow.com/questions/3881857/bitmapsource-copypixels-whats-a-good-value-for-stride

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Jeff0803 Tuesday, March 14, 2017 9:46 PM
    Tuesday, March 14, 2017 6:36 PM
  • Hi,

    the stride is the "scanline" of a bitmap. That's one row in bytes. Since a bitmaps row must always be a multiple of 4, the stride for a normal 32bpp bitmap is 4 * width, but may be another value for eg 24 bpp bitmaps. EG a 24 bpp bitmap of with 3 will have three bytes per pixel times 3 = 9 bytes as witdth, the stride is a multiple of 4, so the stride would be 12. Its the width in bytes plus a possible "offset" that adds the missing bits or bytes to a multiple of 4 of that row.

    Regards,

      Thorsten

    • Edited by Thorsten Gudera Tuesday, March 14, 2017 7:43 PM
    • Marked as answer by Jeff0803 Tuesday, March 14, 2017 9:46 PM
    Tuesday, March 14, 2017 7:43 PM

All replies

  • >>Can anybody explain about this?

    Stride is pixel size in bytes. Please refer to the answer and the comments in the following thread for more information: http://stackoverflow.com/questions/3881857/bitmapsource-copypixels-whats-a-good-value-for-stride

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by Jeff0803 Tuesday, March 14, 2017 9:46 PM
    Tuesday, March 14, 2017 6:36 PM
  • Hi,

    the stride is the "scanline" of a bitmap. That's one row in bytes. Since a bitmaps row must always be a multiple of 4, the stride for a normal 32bpp bitmap is 4 * width, but may be another value for eg 24 bpp bitmaps. EG a 24 bpp bitmap of with 3 will have three bytes per pixel times 3 = 9 bytes as witdth, the stride is a multiple of 4, so the stride would be 12. Its the width in bytes plus a possible "offset" that adds the missing bits or bytes to a multiple of 4 of that row.

    Regards,

      Thorsten

    • Edited by Thorsten Gudera Tuesday, March 14, 2017 7:43 PM
    • Marked as answer by Jeff0803 Tuesday, March 14, 2017 9:46 PM
    Tuesday, March 14, 2017 7:43 PM