gpt4 book ai didi

c# - 从 PixelFormat 获取像素长度

转载 作者:行者123 更新时间:2023-11-30 12:10:16 26 4
gpt4 key购买 nike

如何从 PixelFormat 获取像素长度(以字节为单位)枚举?我想使用 native 方法处理图像像素,但如果我不知道像素的偏移量,我该如何遍历图像。

代码:

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

关于c# - 从 PixelFormat 获取像素长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18835588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com