Get pixel length from PixelFormat

和自甴很熟 提交于 2019-12-23 12:06:59

问题


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

回答1:


Image.GetPixelFormatSize(image.PixelFormat);

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



来源:https://stackoverflow.com/questions/18835588/get-pixel-length-from-pixelformat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!