在C#中将ColorConvertedBitmap转换为字节数组

时间:2010-08-28 12:30:21

标签: c# wpf bitmap

我需要从我的ColorConvertedBitmap中获取表示位图的字节数组。我试图使用CopyPixels方法,但没有成功。

如何完成此任务以及最佳方法是什么?

提前感谢您的回复和提示!

1 个答案:

答案 0 :(得分:1)

public static byte[] BitmapToBytes(ColorConvertedBitmap ccb)
{
    byte[] bytes = new byte[ccb.PixelWidth * ccb.PixelHeight * ccb.Format.BitsPerPixel / 8];
    ccb.CopyPixels(bytes, ccb.PixelWidth * ccb.Format.BitsPerPixel / 8, 0);
    return bytes;
}