C#合并BitMap图像如何生成超大bitmap
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍C#合并BitMap图像如何生成超大bitmap,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!当只需要两个图像合并的时候,可以简单的使用gdi+,把两个图像画到
千家信息网最后更新 2026年11月02日C#合并BitMap图像如何生成超大bitmap
这篇文章主要介绍C#合并BitMap图像如何生成超大bitmap,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
当只需要两个图像合并的时候,可以简单的使用gdi+,把两个图像画到一个画布上面实现合并bitmap.
当需要将许多bitmap合并时,由于bitmap类限制,长度或宽度太大时会报异常,前面这种方法就行不通了。
由于bitmapp属于位图格式,了解图像格式后,发现,bitmap文件的第3-8位存储了文件大小信息,第19-22位存储了高度信息,第23-26位存储了宽度信息。文件头后面都是像素的argb,并无其它信息。于是,试想一下,如果把第二张图像的像素argb放到第一张后面,并修改第一张的文件头信息,是不是就可以实现文件合并了呢。事实证明:yes。
//设置文件头里面文件大小信息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 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);//高度保存位置 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); } } } }合并多个bitmap文件,并生成一个最终文件
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; }以上是"C#合并BitMap图像如何生成超大bitmap"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
文件
信息
图像
生成
宽度
高度
存储
C#
两个
位置
像素
内容
大小
格式
篇文章
长度
成功
事实
价值
位图
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
北京路客互联网科技有限公司
互联网科技垄断
互联网 科技
互联网 科技公司
北京互联网络科技有限公司
互联网科技汛
佰商互联网科技有限公司
海南购全球互联网科技
成都玖崟互联网科技有限公司乐商圈怎么样
常德市电业局互联网科技部
恒生互联网科技指数跳水
亳州好赞互联网科技有限公司
国大互联网教育科技有限公司工作
济南有互联网科技公司吗
领航科技大厦互联网公司
南京一网一红互联网科技有限公司
易企秀互联网科技是做什么的
济南小医互联网科技有限公司招聘
东风日产互联网科技公司叫什么
株洲百竞科技的互联网营销顾问
互联网科技英文
奇士达互联网科技
互联网科技小额贷款
蓝色互联网科技
互联网时代科技改变生活
昊天互联网科技有限公司
华夏恒生互联网科技013171
长沙渺峰互联网科技有限公司
亚光科技低轨卫星互联网
互联网领先科技成果宣传视频