原始像素阵列到灰度BitmapImage [英] raw pixel array to gray scale BitmapImage

查看:52
本文介绍了原始像素阵列到灰度BitmapImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an array of raw pixel data. I would like to convert it into 8bpp Bitmap.

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;
}

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

bitmap looks like a color image instead of grayscale.

推荐答案

在返回位图之前尝试添加以下内容:

Try adding this before you return the bitmap:

for (int c = 0; c < bitmap.Palette.Entries.Length; c++)
    bitmap.Palette.Entries[c] = Color.FromArgb(c, c, c);

它将创建一个典型的灰度调色板.

It will create a typical grayscale palette.

这篇关于原始像素阵列到灰度BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆