【问题标题】:Get pixel length from PixelFormat从 PixelFormat 获取像素长度
【发布时间】:2013-09-17 05:39:55
【问题描述】:

如何从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

【问题讨论】:

  • 您需要为 1bpp 和 4bpp 格式做一些合理的事情。就像拒绝他们一样。

标签: c# .net image-processing f#


【解决方案1】:
Image.GetPixelFormatSize(image.PixelFormat);

返回指定的颜色深度,以每像素的位数为单位 像素格式。 -- Documentation

【讨论】:

  • 位,而不是字节。
  • 除以8得到字节!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-02
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多