6

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
ildjarn
  • 62,044
  • 9
  • 127
  • 211
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59

1 Answers1

5
Image.GetPixelFormatSize(image.PixelFormat);

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

dsfgsho
  • 2,731
  • 2
  • 22
  • 39