When only two images are needed to merge, you can simply use gdi+ to draw the two images onto a canvas to achieve the merge bitmap.
When many bitmap need to be merged, due to bitmap class limitations, an exception will be reported when the length or width is too large. The previous method will not work.
Since bitmap is a bitmap format, after understanding the image format, I found that bitmap file size information is stored, bitmap file size information is stored, bitmap file height information is stored, and bits 23-26 store width information is stored. The file header is followed by pixel argbs, and there is no other information. So, imagine, if you put the pixel argb of the second image behind the first image and modify the file header information of the first image, can you realize the file merging? As it turns out: yes.
//Set file size information in the file header public void SetBitmapFileSizeInfo(string filePath) { FileInfo fileInfo = new FileInfo(filePath); long le = ; string hexSize = ("X").PadLeft(8, '0'); int size1 = Convert.ToInt32((0, 2), 16); int size2 = Convert.ToInt32((2, 2), 16); int size3 = Convert.ToInt32((4, 2), 16); int size4 = Convert.ToInt32((6, 2), 16); byte[] sizeBytes = new byte[] { (byte)size4, (byte)size3, (byte)size2, (byte)size1 }; using (FileStream fs = new FileStream(filePath, , )) { using (BinaryWriter r = new BinaryWriter(fs)) { (2, 0); (sizeBytes, 0, ); } } }
Set file length and width information in the file header
public void SetBitmapSizeInfo(string filePath,int width=0,int height=0) { if (height != 0) { string hexHeight = ("X").PadLeft(8, '0'); int h1 = Convert.ToInt32((0, 2), 16); int h2 = Convert.ToInt32((2, 2), 16); int h3 = Convert.ToInt32((4, 2), 16); int h4 = Convert.ToInt32((6, 2), 16); byte[] sizeHeight = new byte[] { (byte)h4, (byte)h3, (byte)h2, (byte)h1 }; using (FileStream fs = new FileStream(filePath, , )) { using (BinaryWriter r = new BinaryWriter(fs)) { (22, 0);//Height save position (sizeHeight, 0, ); } } } if (width != 0) { string hexWidth = ("X").PadLeft(8, '0'); int w1 = Convert.ToInt32((0, 2), 16); int w2 = Convert.ToInt32((2, 2), 16); int w3 = Convert.ToInt32((4, 2), 16); int w4 = Convert.ToInt32((6, 2), 16); byte[] sizeWidth = new byte[] { (byte)w4, (byte)w3, (byte)w2, (byte)w1 }; using (FileStream fs = new FileStream(filePath, , )) { using (BinaryWriter r = new BinaryWriter(fs)) { (18, 0);//Height save position (sizeWidth, 0, ); } } } }
Merge multiple bitmap files and generate a final file
private void CreateBitMap(string tempPath,string imagePath) { string[] files = (tempPath, "*.png"); Bitmap bmp; int height=0; for (int i = -1; i >0; i--) { string fileName = files[i]; bmp = new Bitmap(fileName); if (i == - 1) { (imagePath, ); height += ; (); continue; } else { byte[] bytes = GetImageRasterBytes(bmp, PixelFormat.Format32bppRgb); using (FileStream fs = new FileStream(imagePath, , )) { (, 0); (bytes, 0, ); } height += ; (); } } SetBitmapFileSizeInfo(imagePath); SetBitmapSizeInfo(imagePath, height: height); //("Merge successful"); } private static byte[] GetImageRasterBytes(Bitmap bmp, PixelFormat format) { Rectangle rect = new Rectangle(0, 0, , ); byte[] bits = null; try { // Lock the managed memory BitmapData bmpdata = (rect, , format); // Declare an array to hold the bytes of the bitmap. bits = new byte[ * ]; // Copy the values into the array. (bmpdata.Scan0, bits, 0, ); // Release managed memory (bmpdata); } catch { return null; } return bits; }
This is the article about C# merge BitMap image generation and generate a huge bitmap. For more related C# merge BitMap content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!