Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate super-large bitmap by merging BitMap images with C#

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to generate super-large bitmap by merging BitMap images with C#. It is very detailed and has a certain reference value. Interested friends must finish reading it!

When only two images are needed to merge, you can simply use gdi+, to draw two images onto a canvas to merge bitmap.

When you need to merge many bitmap, due to the limitation of the bitmap class, an exception is reported when the length or width is too large, so the previous approach does not work.

Because bitmapp belongs to bitmap format, after understanding the image format, it is found that the 3-8 bits of the bitmap file store the file size information, the 19-22 bits store the height information, and the 23-26 bits store the width information. The file header is followed by pixel argb, and there is no other information. So, imagine, if you put the pixel argb of the second image behind the first, and modify the header information of the first, is it possible to merge files? It turns out that yes.

/ / set the file size information in the header 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);}

Set the 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 = height.ToString ("X"). PadLeft (8,'0'); int H2 = Convert.ToInt32 (hexHeight.Substring (0,2), 16); int h3 = Convert.ToInt32 (hexHeight.Substring (2,2), 16) Int h4 = Convert.ToInt32 (hexHeight.Substring (4,2), 16); int h5 = Convert.ToInt32 (hexHeight.Substring (6,2), 16); byte [] sizeHeight = new byte [] {(byte) h5, (byte) h4, (byte) h3, (byte) H2} Using (FileStream fs = new FileStream (filePath, FileMode.Open, FileAccess.ReadWrite)) {using (BinaryWriter r = new BinaryWriter (fs)) {r.Seek (22,0); / / highly saved location 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); / / highly saved location r.Write (sizeWidth, 0, sizeWidth.Length) }

Merge multiple bitmap files and generate a final file

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; iMurray -) {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 ("merge successfully") } 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 } these are all the contents of the article "how to generate super-large bitmap by merging BitMap images with C#". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report