从 PixelFormat 获取像素长度

Dzm*_*voi 5 .net c# f# image-processing

如何从PixelFormat枚举中获取像素长度(以字节为单位)?我想使用本机方法处理图像像素,但是如果我不知道像素的偏移量,我如何遍历图像。

代码:

let formRgbCube (image : Bitmap) =
    let width = image.Width
    let height = image.Height
    let bitmapData = image.LockBits(Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat)
    let ptr = NativePtr.ofNativeInt<byte>(bitmapData.Scan0)
    let stride = bitmapData.Stride

    let rgbCube = Array3D.zeroCreate width height 3
    for i = 0 to width - 1 do
        for j = 0 to height - 1 do
            for k = 0 to 2 do
                rgbCube.[i, j, k] <- NativePtr.read<byte>(NativePtr.add ptr (stride * j + i * 3(*an example for 24ppb*) + k))
    rgbCube
Run Code Online (Sandbox Code Playgroud)

dsf*_*sho 5

Image.GetPixelFormatSize(image.PixelFormat);
Run Code Online (Sandbox Code Playgroud)

返回指定像素格式的颜色深度(以每像素的位数表示)。--文档

  • 除以 8 得到字节!! (4认同)