コード例 #1
0
ファイル: UBmp.cs プロジェクト: yhuse/SunnyUI
        /// <summary>
        /// 慢于 bitmap.Save(XX,ImageFormat.Bmp),只是为了解释BMP文件数据格式
        /// </summary>
        /// <param name="bitmap"></param>
        public BmpFile(Bitmap bitmap)
        {
            head = new BmpHead();
            head.Init(bitmap);
            data = new byte[head.FileSize];
            Array.Copy(head.StructToBytes(), 0, data, 0, (int)head.BitmapDataOffset);

            var sourceArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);

            byte[] tmp = new byte[bitmap.Width * bitmap.Height * 4];
            Marshal.Copy(bitmapData.Scan0, tmp, 0, tmp.Length);
            bitmap.UnlockBits(bitmapData);

            //BMP文件的数据从左下角开始,每行向上。System.Drawing.Bitmap数据是从左上角开始,每行向下
            int idx = (int)head.BitmapDataOffset;

            for (int i = 0; i < bitmap.Height; i++)
            {
                int offset = bitmap.Height - 1 - i;
                offset *= bitmap.Width * 4;

                for (int j = 0; j < bitmap.Width; j++)
                {
                    Array.Copy(tmp, offset + j * 4, Data, idx, 3);
                    idx += 3;
                }

                idx += bitmap.Width % 4;
            }
        }
コード例 #2
0
ファイル: UBmp.cs プロジェクト: yhuse/SunnyUI
        /// <summary>
        /// 从图像数据创建Bmp图片
        /// </summary>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <param name="bmpData">数据,从左上角开始,为每个点的B、G、R循环</param>
        public BmpFile(int width, int height, byte[] bmpData)
        {
            head = new BmpHead();
            head.Init(width, height);
            data = new byte[head.FileSize];
            Array.Copy(head.StructToBytes(), 0, data, 0, (int)head.BitmapDataOffset);
            if (bmpData.Length != width * height * 3)
            {
                return;
            }

            //BMP文件的数据从左下角开始,每行向上。System.Drawing.Bitmap数据是从左上角开始,每行向下
            int idx = (int)head.BitmapDataOffset;

            for (int i = 0; i < height; i++)
            {
                int offset = height - 1 - i;
                offset *= width * 3;
                Array.Copy(bmpData, offset, data, idx, width * 3);
                idx += width * 3;
            }
        }