TIFF压缩

时间:2017-09-06 14:37:54

标签: c# .net compression tiff lzw

我正在尝试压缩一些TIFF文件。它们处于某种地图结构中,因此它们应该保留。使用下面的代码,我能够转换每个子目录中的每个第一个文件(名为1.tiff),但是当子目录中有多个文件时,它会给我System.ArgumentException,或者如果名称是文件不是1.tiff。

using RasterEdge.Imaging.Basic;
using RasterEdge.XDoc.TIFF;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace LZWCompression
{
    class Program
    {
        static int temp = 0;

        static void Main(string[] args)
        {
            string root = @"C:x\y\";
            string path = "z";

            Console.WriteLine(Directory.Exists(root + path));

            if (Directory.Exists(root + path))
            {
                // This path is a directory
                ProcessDirectory(root + path, root);
                Console.WriteLine(root + path);
            }
            Console.WriteLine("DONE");
            Console.ReadKey();
        }
        // Process all files in the directory passed in, recurse on any directories 
        // that are found, and process the files they contain.
        public static void ProcessDirectory(string targetDirectory, string targetPath)
        {
            // Process the list of files found in the directory.
            string[] fileEntries = Directory.GetFiles(targetDirectory);

            foreach (string fileName in fileEntries)
            {
                Console.WriteLine(fileName);
                ProcessFile(fileName, targetPath);
            }
            // Recurse into subdirectories of this directory.
            string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);

            foreach (string subdirectory in subdirectoryEntries)
                ProcessDirectory(subdirectory, targetPath);
        }

        public static void ProcessFile(string path, string root)
        {
            string filename = "";
            string location = "";
            temp++;
            List<Bitmap> images = new List<Bitmap>();

            int posFilename = path.LastIndexOf("\\") + 1;
            int posRoot = root.Length;

            filename = path.Substring(posFilename, path.Length - posFilename);
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(800, 1000);

            Graphics g = Graphics.FromImage(bitmap);

            posFilename = posFilename - (posRoot + 1);
            location = path.Substring(posRoot, posFilename);

            Bitmap myBitmap;
            myBitmap = new Bitmap(filename);
            ImageCodecInfo myImageCodecInfo;
            myImageCodecInfo = GetEncoderInfo(filename);
            System.Drawing.Imaging.Encoder myEncoder;
            myEncoder = System.Drawing.Imaging.Encoder.Compression;
            EncoderParameters myEncoderParameters;
            myEncoderParameters = new EncoderParameters(1);
            EncoderParameter myEncoderParameter;
            myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionLZW);
            myEncoderParameters.Param[0] = myEncoderParameter;
            myBitmap.Save(path, ImageFormat.Tiff);

            Console.WriteLine("myBitmap: " + myBitmap.ToString());
            Bitmap bm = (Bitmap)Bitmap.FromFile(path);
            bm = ChangeColor(bm);

            images.Add(bm);

            TIFFDocument doc = new TIFFDocument(images.ToArray(), ImageCompress.LZW);

            if (null == doc)
                throw new Exception("Fail to construct TIFF Document");

            doc.Save(path);
        }

        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }

        public static Bitmap ChangeColor(Bitmap original)
        {
            //create a blank bitmap the same size as original
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            //get a graphics object from the new image
            Graphics g = Graphics.FromImage(newBitmap);

            //create the grayscale ColorMatrix
            ColorMatrix colorMatrix = new ColorMatrix(
               new float[][]
               {
         new float[] {.3f, .3f, .3f, 0, 0},
         new float[] {.59f, .59f, .59f, 0, 0},
         new float[] {.11f, .11f, .11f, 0, 0},
         new float[] {0, 0, 0, 1, 0},
         new float[] {0, 0, 0, 0, 1}
               });

            ImageAttributes attributes = new ImageAttributes();

            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
               0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

            //dispose the Graphics object
            g.Dispose();
            return newBitmap;
        }
    }
}

0 个答案:

没有答案