图片的 rgb信息 byte[] 直接转换为bmp文件

方法1:


        /// <summary>
        /// rgb像素值转换为bmp文件
        /// </summary>
        /// <param name="buffer">rgb像素数组</param>
        /// <param name="width">图片宽</param>
        /// <param name="height">图片高</param>
        /// <param name="filename">bmp文件名</param>
        /// note:
        ///     好像说buffer存的图片长、宽都要是4的倍数,我这里没做适配,可能不是4的倍数可能bmp文件会有问题
        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();
                }
            }
        }

方法2:使用BitmapData,用Marshal.Copy将直接复制到BitmapData中,然后返回就是bitmap对象,直接save

这个一大堆,到处都是

原文地址:https://www.cnblogs.com/FlyonGrass/p/14837172.html