C#ビットマップは、大きなビットマップを生成し、画像合成しました

複数の画像をマージし、C#を使用して、ビットマップ

唯一の2枚の画像を組み合わせると、あなたは単純に組み合わせたトップキャンバスビットマップを達成するために2枚の画像を描画し、GDI +を使用することができます。
長さや幅が大きすぎると異常報告される、ので、それはビットマップの数、ビットマップベースの制限をマージすることが望まれる場合、この方法は、前面を動作しません。

bitmappは高度情報のビットマップ形式、理解FOUND後の画像形式、ビットマップファイル3-8格納ファイルサイズ情報、ビット19-22に属するので、情報を格納された23~26ビット幅、記憶されています。ヘッダに続いて、画素ARGB、他の情報です。だから、最初に画像ARGBバック、およびファイルヘッダ情報の第1の変形例の第2の画素ならば、あなたはファイルを達成することができます想像しては、まだマージされていません。事実があることを証明している:はい。

//ファイルサイズ内のファイルヘッダ情報を設定します

public void SetBitmapFileSizeInfo(string filePath)
        {
            FileInfo fileInfo = new FileInfo(filePath);
            long le = fileInfo.Length;
            string hexSize = le.ToString("X").PadLeft(8, '0');
            int size1 = Convert.ToInt32(hexSize.Substring(0, 2), 16);
            int size2 = Convert.ToInt32(hexSize.Substring(2, 2), 16);
            int size3 = Convert.ToInt32(hexSize.Substring(4, 2), 16);
            int size4 = Convert.ToInt32(hexSize.Substring(6, 2), 16);
            byte[] sizeBytes = new byte[] { (byte)size4, (byte)size3, (byte)size2, (byte)size1 };
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write))
            {
                using (BinaryWriter r = new BinaryWriter(fs))
                {
                    r.Seek(2, 0);
                    r.Write(sizeBytes, 0, sizeBytes.Length);
                }
            }
        }

ヘッダ長さと幅情報ファイル内で設定してください

 public void SetBitmapSizeInfo(string filePath,int width=0,int height=0)
        {
            if (height != 0)
            {
                string hexHeight = height.ToString("X").PadLeft(8, '0');
                int h1 = Convert.ToInt32(hexHeight.Substring(0, 2), 16);
                int h2 = Convert.ToInt32(hexHeight.Substring(2, 2), 16);
                int h3 = Convert.ToInt32(hexHeight.Substring(4, 2), 16);
                int h4 = Convert.ToInt32(hexHeight.Substring(6, 2), 16);
                byte[] sizeHeight = new byte[] { (byte)h4, (byte)h3, (byte)h2, (byte)h1 };
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
                {
                    using (BinaryWriter r = new BinaryWriter(fs))
                    {
                        r.Seek(22, 0);//高度保存位置
                        r.Write(sizeHeight, 0, sizeHeight.Length);
                    }
                }
            }
            if (width != 0)
            {
                string hexWidth = height.ToString("X").PadLeft(8, '0');
                int w1 = Convert.ToInt32(hexWidth.Substring(0, 2), 16);
                int w2 = Convert.ToInt32(hexWidth.Substring(2, 2), 16);
                int w3 = Convert.ToInt32(hexWidth.Substring(4, 2), 16);
                int w4 = Convert.ToInt32(hexWidth.Substring(6, 2), 16);
                byte[] sizeWidth = new byte[] { (byte)w4, (byte)w3, (byte)w2, (byte)w1 };
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
                {
                    using (BinaryWriter r = new BinaryWriter(fs))
                    {
                        r.Seek(18, 0);//高度保存位置
                        r.Write(sizeWidth, 0, sizeWidth.Length);
                    }
                }
            }
        }

複数のビットマップファイルを結合し、最終的なドキュメントを生成

private void CreateBitMap(string tempPath,string imagePath)
        {
            string[] files = Directory.GetFiles(tempPath, "*.png");
            Bitmap bmp;
            int height=0;
            for (int i = files.Length-1; i >0; i--)
            {
                string fileName = files[i];
                bmp = new Bitmap(fileName);
                if (i == files.Length - 1)
                {
                    bmp.Save(imagePath, ImageFormat.Bmp);
                    height += bmp.Height;
                    bmp.Dispose();
                    continue;
                }
                else
                {
                    byte[] bytes = GetImageRasterBytes(bmp, PixelFormat.Format32bppRgb);
                    using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Write))
                    {
                        fs.Seek(fs.Length, 0);
                        fs.Write(bytes, 0, bytes.Length);
                    }
                    height += bmp.Height;
                    bmp.Dispose();
                }
            }
            SetBitmapFileSizeInfo(imagePath);
            SetBitmapSizeInfo(imagePath, height: height);
            //MessageBox.Show("合并成功");
        }
         private static byte[] GetImageRasterBytes(Bitmap bmp, PixelFormat format)
        {
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            byte[] bits = null;
            try
            {
                // Lock the managed memory
                BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.ReadWrite, format);
                // Declare an array to hold the bytes of the bitmap.
                bits = new byte[bmpdata.Stride * bmpdata.Height];
                // Copy the values into the array.
                System.Runtime.InteropServices.Marshal.Copy(bmpdata.Scan0, bits, 0, bits.Length);
                // Release managed memory
                bmp.UnlockBits(bmpdata);
            }
            catch
            {
                return null;
            }
            return bits;
        }
リリース6元記事 ウォンの賞賛3 ビュー8403

おすすめ

転載: blog.csdn.net/qq_35669328/article/details/100696671