Why must “stride” in the System.Drawing.Bitmap constructor be a multiple of 4?

后端 未结 6 1605
悲&欢浪女
悲&欢浪女 2020-11-22 12:32

I am writing an application that requires me to take a proprietary bitmap format (an MVTec Halcon HImage) and convert it into a System.Drawing.Bitmap in C#.

The only

6条回答
  •  心在旅途
    2020-11-22 12:44

    Correct code:

    public static void GetStride(int width, PixelFormat format, ref int stride, ref int bytesPerPixel)
    {
        //int bitsPerPixel = ((int)format & 0xff00) >> 8;
        int bitsPerPixel = System.Drawing.Image.GetPixelFormatSize(format);
        bytesPerPixel = (bitsPerPixel + 7) / 8;
        stride = 4 * ((width * bytesPerPixel + 3) / 4);
    }
    

提交回复
热议问题