gpt4 book ai didi

c# - 原始像素数组到灰度 BitmapImage

转载 作者:行者123 更新时间:2023-11-30 14:09:28 27 4
gpt4 key购买 nike

我有一组原始像素数据。我想将其转换为 8bpp 位图。

public static Bitmap ByteToGrayBitmap(byte[] rawBytes, int width, int height) 
{
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, bitmap.PixelFormat);

Marshal.Copy(rawBytes, 0, bitmapData .Scan0, rawBytes.Length);
bitmap.UnlockBits(bitmapData);

return bitmap;
}

位图看起来像彩色图像而不是灰度图像。

最佳答案

您的图像需要一个 8 位灰度调色板。

返回前添加:

var pal = bmp.Palette;
for (int i = 0; i < 256; i++) pal.Entries[i] = Color.FromArgb(i, i, i);
bmp.Palette = pal;

return bmp;

关于c# - 原始像素数组到灰度 BitmapImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29087075/

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