使用c#,合并多个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 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);}}}}

合并多个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 memoryBitmapData 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 memorybmp.UnlockBits(bmpdata);}
catch{
return null;}
return bits;}

完成喽。

如果喜欢,点个赞呗~

C# 合并BitMap图像,生成超大bitmap相关推荐

  1. VB6 在内存中生成bitmap图像,并将其保存为byte()数组

    在VB6内存中创建bitmap图像,并将其保存为byte()数组 在项目中添加一个模块,用于封装GDIPlus函数 下载地址: gdiplus函数封装 从内存中创建相应的信息 '全局声明 Dim to ...

  2. 【Android 内存优化】Bitmap 图像尺寸缩小 ( 考虑像素密度、针对从不同像素密度资源中解码对应的 Bitmap 对象 | inDensity | inTargetDensity )

    文章目录 一.像素密度对解码图片的影响 二.不考虑像素密度会导致图片缩小尺寸不准确 三.DisplayMetrics 源码阅读.研究手机资源获取规则 四.像素密度参数设置取值 ( inDensity ...

  3. 【Android 内存优化】Bitmap 图像尺寸缩小 ( 设置 Options 参数 | inJustDecodeBounds | inSampleSize | 工具类实现 )

    文章目录 一.解码图片参数 inJustDecodeBounds 二.计算图片的缩小比例 三.设置图片缩小配置 inSampleSize 四.设置图片像素格式 inPreferredConfig 五. ...

  4. BMP(图像文件格式(Bitmap))

    BMP(全称Bitmap)是Windows操作系统中的标准图像文件格式,可以分成两类:设备相关位图(DDB)和设备无关位图(DIB),使用非常广.它采用位映射存储格式,除了图像深度可选以外,不采用其他 ...

  5. 【Android】Bitmap图像色彩模式:黑白、模糊、老照片、胶卷等(92/100)

    图像转换封装工具类BitmapUtil: /*** 图片位图转换工具** @author lichong* 2022年07月26日15:35:16*/ public class BitmapUtil ...

  6. Android Bitmap图像优化

    试一试:点击下载. 在Android应用开发中不可避免的会用到图形图像,这样就会生成Bitmap对象.如果在开发过程中没有处理好Bitmap对象就很容易产生Out Of Memory(OOM)的异常. ...

  7. android平台下基于ANativeWindow实现渲染bitmap图像

    OpenGL ES 3.0学习实践 android平台下OpenGL ES 3.0从零开始 android平台下OpenGL ES 3.0绘制纯色背景 android平台下OpenGL ES 3.0绘 ...

  8. 位图BitMap图像的读取与存储

    做图像处理时的源文件一般要用无损的图像文件格式,位图(BitMap)是windows系统下可存储无压缩图像的文件格式.要实现位图文件的读取和存储,首先要明白位图文件的的存储数据结构.位图文件由四部分依 ...

  9. Pixel2Mesh从单个RGB图像生成三维网格ECCV2018

    目录 摘要 1.Introduction 2.Related Work 3.Method 3.1.准备工作:基于图的卷积 3.2.系统概述 3.3.初始椭球 3.4.Mesh deformation ...

最新文章

  1. AE实现不同图层的合并C#代码
  2. 【问题记录】raise IndexError(‘index {} is out of range‘.format(idx)) index 0 is out of range
  3. PHP-FPM对比Swoole:Swoole多了Reactor线程监听Socket 句柄的变化 代码初始化一次不结束进程 ws tcp mqtt服务
  4. php有哪些高级扩展,php扩展有哪些
  5. Logistic回归模型原理
  6. log4j mysql 单引号_log4j写数据库存在单引号问题
  7. 扩展欧几里得算法(双六游戏)
  8. 大智慧 软件 开发语言_智慧工厂培训软件开发流程篇
  9. Matcher的group()/group(int group)/groupCount()用法介绍
  10. 《翻译与本地化CAT软件实用教程》目录
  11. 很少人用的下载者方法
  12. 搭建无线打印服务器,用旧电脑轻松架设无线网络打印服务器
  13. 百度翻译api和SpringBoot集成
  14. [swift] UIImage NSImage PNG透明区域填充自定义颜色实现
  15. 高德地图 搜索店名获取经纬度
  16. 【计算机网络】PPP和PPPoE协议
  17. 如何看hbo_哪些设备支持HBO Max? Roku和Amazon Fire TV不要
  18. java jdk15.0.1环境配置(图文教程)
  19. POI 实现Word替换文本2种情况(正常文本、表格文本)
  20. uniapp应用和页面生命周期

热门文章

  1. 30万手表推荐_今年六十岁生日,儿子说要送只30万的手表,请问有哪些推荐?...
  2. asp.net core结合NLog搭建ELK实时日志分析平台
  3. ubantu 重启mysql
  4. DataGridView很详细的用法
  5. 各个版本spring的jar包以及源码下载地址
  6. 【原创】SQL SERVER 查询Job作业基本信息及执行情况
  7. Oracle9i卸载后再次安装,设置的SID相同出现“指定的SID在本机上已经存在。请指定一个不同的SID。”...
  8. 求连续序列的最大子序列和
  9. 分布式服务下的关键技术(转)
  10. 多云战略:企业如何精益求精?