Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 685
Dzmitry Martavoi Avatar asked Oct 02 '22 22:10

Dzmitry Martavoi


1 Answers

Image.GetPixelFormatSize(image.PixelFormat);

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

like image 129
dsfgsho Avatar answered Oct 13 '22 12:10

dsfgsho