C# 将多个图片合并成TIFF文件的两种方法

2021/5/13 22:31:02

本文主要是介绍C# 将多个图片合并成TIFF文件的两种方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

最近需要用到TIF格式的文件,研究了一段时间,终于有点结果了,

发现两种方式,第一种是使用BitMiracle.LibTiff.NET,直接在Nuget上安装即可

,第二种是使用RasterEdge.DocImageSDK,要从官网下载dll包

第一种免费,但是生成的tiff文件大小比原始图片大的多

第二种收费,但是有试用期一个月,效果很好,生成的tiff文件比原图小的多而且不失真。过期之后,只需要到官网下载最新dll,重新引用即可再来一个月试用。。。

先说第二种RasterEdge.DocImageSDK的使用方法:

官网地址:http://www.rasteredge.com/how-to/csharp-imaging/tiff-convert-bmp/
//过期后打开上面 这个网址,重新下载 dll包,重新引用即可 bin x64 4.0

新建一个cmd项目,测试代码如下:

class Program
    {
        private static byte[] CompressionImage(Stream fileStream, long quality)
        {
            using (System.Drawing.Image img = System.Drawing.Image.FromStream(fileStream))
            {
                using (Bitmap bitmap = new Bitmap(img))
                {
                    ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat);
                    System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                    EncoderParameters myEncoderParameters = new EncoderParameters(1);
                    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
                    myEncoderParameters.Param[0] = myEncoderParameter;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitmap.Save(ms, CodecInfo, myEncoderParameters);
                        myEncoderParameters.Dispose();
                        myEncoderParameter.Dispose();
                        return ms.ToArray();
                    }
                }
            }
        }
        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                { return codec; }
            }
            return null;
        }
        static void Main(string[] args)
        {
            string[] imagePaths = System.IO.Directory.GetFiles(@"D:\images\","*.jpg");
            
            Bitmap[] bmps = new Bitmap[imagePaths.Count()];
            for (int i = 0; i < imagePaths.Length; i++)
            {
                var stream = new FileStream(imagePaths[i], FileMode.Open);
                var by = CompressionImage(stream, 100);
                stream.Close();

                Bitmap tmpBmp = new Bitmap(new MemoryStream(by));

                if (tmpBmp != null)
                    bmps[i] = tmpBmp;
            }
            ImageOutputOption option = new ImageOutputOption() { Color = ColorType.Color, Compression = ImageCompress.CCITT };
            TIFFDocument tifDoc = new TIFFDocument(bmps, option);
            if (tifDoc == null)
                throw new Exception("Fail to construct TIFF Document");
            tifDoc.Save(@"D:\images\test.tif");
        }
    
    }

图片

下面说说第二种免费的方式:

新建一个TiffHelper帮助类:

using BitMiracle.LibTiff.Classic;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace JpgToTiff
{
    public class UserErrorHandler : TiffErrorHandler
    {
        public override void WarningHandler(Tiff tif, string method, string format, params object[] args)
        {
            //base.WarningHandler(tif, method, format, args);
        }
        public override void WarningHandlerExt(Tiff tif, object clientData, string method, string format, params object[] args)
        {
            //base.WarningHandlerExt(tif, clientData, method, format, args);
        }
    }
    public class TiffHelper
    {
        public static UserErrorHandler m_errorHandler = new UserErrorHandler();
        static TiffHelper()
        {
            Tiff.SetErrorHandler(m_errorHandler);
        }
        /// <summary>
        /// 合并jpg
        /// </summary>
        /// <param name="bmps">bitmap数组</param>
        /// <param name="tiffSavePath">保存路径</param>
        /// <param name="quality">图片质量,1-100</param>
        /// <returns></returns>
        public static bool Jpegs2Tiff(Bitmap[] bmps, string tiffSavePath, int quality = 15)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                using (Tiff tif = Tiff.ClientOpen(@"in-memory", "w", ms, new TiffStream()))
                {
                    foreach (var bmp in bmps)//
                    {
                        byte[] raster = GetImageRasterBytes(bmp, PixelFormat.Format24bppRgb);
                        tif.SetField(TiffTag.IMAGEWIDTH, bmp.Width);
                        tif.SetField(TiffTag.IMAGELENGTH, bmp.Height);
                        tif.SetField(TiffTag.COMPRESSION, Compression.JPEG);
                        tif.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB);
                        tif.SetField(TiffTag.JPEGQUALITY, quality);
                        tif.SetField(TiffTag.ROWSPERSTRIP, bmp.Height);

                        tif.SetField(TiffTag.XRESOLUTION, 90);
                        tif.SetField(TiffTag.YRESOLUTION, 90);

                        tif.SetField(TiffTag.BITSPERSAMPLE, 8);
                        tif.SetField(TiffTag.SAMPLESPERPIXEL, 3);

                        tif.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);

                        int stride = raster.Length / bmp.Height;
                        ConvertSamples(raster, bmp.Width, bmp.Height);

                        for (int i = 0, offset = 0; i < bmp.Height; i++)
                        {
                            tif.WriteScanline(raster, offset, i, 0);
                            offset += stride;
                        }

                        tif.WriteDirectory();
                    }
                    System.IO.FileStream fs = new FileStream(tiffSavePath, FileMode.Create);
                    
                    ms.Seek(0, SeekOrigin.Begin);
                    fs.Write(ms.ToArray(), 0, (int)ms.Length);
                    fs.Close();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        private static byte[] GetImageRasterBytes(Bitmap bmp, PixelFormat format)
        {
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            byte[] bits = null;
            try
            {
                BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.ReadWrite, format);
                bits = new byte[bmpdata.Stride * bmpdata.Height];
                System.Runtime.InteropServices.Marshal.Copy(bmpdata.Scan0, bits, 0, bits.Length);
                bmp.UnlockBits(bmpdata);
            }
            catch
            {
                return null;
            }
            return bits;
        }
        private static void ConvertSamples(byte[] data, int width, int height)
        {
            int stride = data.Length / height;
            const int samplesPerPixel = 3;

            for (int y = 0; y < height; y++)
            {
                int offset = stride * y;
                int strideEnd = offset + width * samplesPerPixel;

                for (int i = offset; i < strideEnd; i += samplesPerPixel)
                {
                    byte temp = data[i + 2];
                    data[i + 2] = data[i];
                    data[i] = temp;
                }
            }
        }
    }
}

下面是测试代码:

class Program
    {
        private static byte[] CompressionImage(Stream fileStream, long quality)
        {
            using (System.Drawing.Image img = System.Drawing.Image.FromStream(fileStream))
            {
                using (Bitmap bitmap = new Bitmap(img))
                {
                    ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat);
                    System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                    EncoderParameters myEncoderParameters = new EncoderParameters(1);
                    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
                    myEncoderParameters.Param[0] = myEncoderParameter;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitmap.Save(ms, CodecInfo, myEncoderParameters);
                        myEncoderParameters.Dispose();
                        myEncoderParameter.Dispose();
                        return ms.ToArray();
                    }
                }
            }
        }
        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                { return codec; }
            }
            return null;
        }
        static void Main(string[] args)
        {
            string[] imagePaths = System.IO.Directory.GetFiles(@"D:\images\","*.jpg");
            
            Bitmap[] bmps = new Bitmap[imagePaths.Count()];
            for (int i = 0; i < imagePaths.Length; i++)
            {
                var stream = new FileStream(imagePaths[i], FileMode.Open);
                var by = CompressionImage(stream, 100);
                stream.Close();

                Bitmap tmpBmp = new Bitmap(new MemoryStream(by));

                if (tmpBmp != null)
                    bmps[i] = tmpBmp;
            }
            TiffHelper.Jpegs2Tiff(bmps, @"D:\images\test.tif", 100);
        }   
    }

图片

可以看到,两个方式生成的tif文件大小简直天壤之别。。。

7个原图大小4.8M,第一种1.36M,

第二种直接23.5M…

也可能是我没有弄好压缩方式。。。。

那我就不晓得了。



这篇关于C# 将多个图片合并成TIFF文件的两种方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程