服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#合并BitMap图像生成超大bitmap

C#合并BitMap图像生成超大bitmap

2022-12-08 11:49记住我Gxy C#

当两个图像合并的时候,以简单的使用gdi+,当需要将许多bitmap合并时就会造成宽度过大,那么怎么实现C#合并BitMap图像,本文就详细的介绍一下

当只需要两个图像合并的时候,可以简单的使用gdi+,把两个图像画到一个画布上面实现合并bitmap.

当需要将许多bitmap合并时,由于bitmap类限制,长度或宽度太大时会报异常,前面这种方法就行不通了。

由于bitmapp属于位图格式,了解图像格式后,发现,bitmap文件的第3-8位存储了文件大小信息,第19-22位存储了高度信息,第23-26位存储了宽度信息。文件头后面都是像素的argb,并无其它信息。于是,试想一下,如果把第二张图像的像素argb放到第一张后面,并修改第一张的文件头信息,是不是就可以实现文件合并了呢。事实证明:yes。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//设置文件头里面文件大小信息
 
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);
                }
            }
        }

设置文件头里面文件长度和宽度信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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文件,并生成一个最终文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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#合并BitMap内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_35669328/article/details/100696671

延伸 · 阅读

精彩推荐
  • C#c# 网络编程之tcp

    c# 网络编程之tcp

    这篇文章主要介绍了c# 网络编程之tcp的的相关资料,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...

    seabluescn8632022-11-01
  • C#C#中感叹号(!) 的作用总结

    C#中感叹号(!) 的作用总结

    这篇文章主要给大家总结介绍了C#中感叹号(!) 的作用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    李志强11612022-10-20
  • C#浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题

    浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题

    下面小编就为大家分享一篇浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题,具有很好的参考价值,希望对大家有所帮助...

    绛河7672022-02-10
  • C#C#中ManualResetEvent用法总结

    C#中ManualResetEvent用法总结

    这篇文章主要介绍了C#中ManualResetEvent用法总结,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    旷野风筝少年4222022-10-31
  • C#C#中public变量不能被unity面板识别的解决方案

    C#中public变量不能被unity面板识别的解决方案

    这篇文章主要介绍了C#中public变量不能被unity面板识别的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    胜天半子_王二_王半仙4972022-11-14
  • C#C#中的==运算符

    C#中的==运算符

    这篇文章主要介绍了C#中的==运算符,非常不错,具有参考借鉴价值,需要的朋友可以参考下...

    Sweet-Tang8182022-01-11
  • C#C#调用webservice接口的最新方法教程

    C#调用webservice接口的最新方法教程

    webservice 可以用于分布式应用程序之间的交互,和不同程序之间的交互。下面这篇文章主要给大家介绍了关于C#调用webservice接口的相关资料,文中通过图文...

    上青天揽月4172022-02-10
  • C#c# 三种方法调用WebService接口

    c# 三种方法调用WebService接口

    这篇文章主要介绍了c# 三种方法调用WebService接口的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    逛园子$$$9072022-09-22