RGB的byte[]怎么转换为bmp文件?
RGB的byte[]怎么转换为bmp文件?
要确定可以用的
https://www.cnblogs.com/klkucan/archive/2012/11/19/2777628.html 这个不行,问题不知道在哪里
C#
回答
//最简单的,没有做异常处理,传入rgb的byte数组,然后保存为 filename 文件
// 按道理是要处理图片的长和宽的,看看是不是4的倍数,不是4的倍数则补成4的倍数
public static void ConvertRGB2Bmp(byte[] buffer, int width, int height, string filename)
{
using (FileStream fs = File.Open(filename, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
bw.Write('B');
bw.Write('M');//BM 2
bw.Write(40 + 14 + buffer.Length);//SIZE 4
bw.Write((ushort)0);//bfReserved1 2
bw.Write((ushort)0);//bfReserved2 2
bw.Write(40 + 14); //4
bw.Write(40); //4
bw.Write(width);
bw.Write(height);
bw.Write((ushort)1);
bw.Write((ushort)24);
bw.Write(0);
bw.Write(buffer.Length);
bw.Write(0);
bw.Write(0);
bw.Write(0);
bw.Write(0);
bw.Write(buffer, 0, buffer.Length);
bw.Flush();
}
}
}
结果,上黑是因为我的byte[]未全部初始化,默认都是0,然后下半截白是因为我读取的图片转化的rgb数组很奇怪,不仅少,而且还不是3的倍数(24位)
