简体   繁体   中英

Get pixel length from PixelFormat

How can i get pixel length (in bytes) from PixelFormat enumeration? I want to process image pixels using native approach, but how can i iterate through an image if i don't know pixel's offset.

Code:

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
Image.GetPixelFormatSize(image.PixelFormat);

Returns the color depth, in number of bits per pixel, of the specified pixel format. -- Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM