I am trying to compress some TIFF files. These are in a certain map structure, and so they should stay. With the code bellow, I am able to convert every first file (named 1.tiff) in every subdirectory, but it gives me a System.ArgumentException
when there are more than one file in a subdirectory, or if the name of the file is not 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;
}
}
}