|
using Aspose.Imaging;
|
|
using Aspose.Imaging.FileFormats.Emf;
|
|
using Aspose.Imaging.FileFormats.Png;
|
|
using Aspose.Imaging.FileFormats.Tiff;
|
|
using Aspose.Imaging.FileFormats.Tiff.Enums;
|
|
using Aspose.Imaging.ImageOptions;
|
|
using Aspose.Imaging.Sources;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
|
|
string dataDir = templatesFolder;
|
|
|
|
Run();
|
|
|
|
//------------------------------------------------------------------------
|
|
// Compression plug-in use sample
|
|
//------------------------------------------------------------------------
|
|
void Run()
|
|
{
|
|
// Valid compression plug-in use sample
|
|
Metered license = new Metered();
|
|
// Only metered plug-in license is supported
|
|
license.SetMeteredKey("<your public key>", "<your private key>");
|
|
|
|
foreach (var inputFile in Directory.GetFiles(Path.Combine(templatesFolder, "*.jpg"))
|
|
{
|
|
using (var image = Image.Load(inputFile))
|
|
{
|
|
GetCompressionOptions(FileFormat.Png, image).ForEach(options =>
|
|
{
|
|
ImageOptionsBase oBase = options.Item1;
|
|
string currentOutputFile = outputFile;
|
|
if (options.Item2 != null)
|
|
{
|
|
currentOutputFile += $"{options.Item2}";
|
|
}
|
|
|
|
doCompress(currentOutputFile, image, options.Item1);
|
|
}
|
|
);
|
|
}
|
|
|
|
BaseInputOutputClass.RemoveFile(outputFile);
|
|
}
|
|
}
|
|
|
|
List<Tuple<ImageOptionsBase, string>> GetCompressionOptions(FileFormat fileFormat, Image image)
|
|
{
|
|
Func<VectorRasterizationOptions> rasterizationOptionsFactory = () =>
|
|
{
|
|
switch (image.FileFormat)
|
|
{
|
|
case FileFormat.Cdr:
|
|
return new CdrRasterizationOptions() { PageWidth = image.Width, PageHeight = image.Height };
|
|
case FileFormat.Cmx:
|
|
return new CmxRasterizationOptions() { PageWidth = image.Width, PageHeight = image.Height };
|
|
case FileFormat.Odg:
|
|
return new OdgRasterizationOptions() { PageWidth = image.Width, PageHeight = image.Height };
|
|
case FileFormat.Otg:
|
|
return new OtgRasterizationOptions() { PageWidth = image.Width, PageHeight = image.Height };
|
|
case FileFormat.Eps:
|
|
return new EpsRasterizationOptions() { PageWidth = image.Width, PageHeight = image.Height };
|
|
default:
|
|
return null;
|
|
}
|
|
};
|
|
|
|
switch (fileFormat)
|
|
{
|
|
case FileFormat.Apng:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>> {
|
|
new Tuple<ImageOptionsBase, string>(new ApngOptions
|
|
{
|
|
CompressionLevel = 9,
|
|
Progressive = true,
|
|
ColorType = PngColorType.IndexedColor,
|
|
Palette = ColorPaletteHelper.GetCloseImagePalette((RasterImage)image, 1 << 8)
|
|
},null)
|
|
};
|
|
}
|
|
case FileFormat.Bmp:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
{ new Tuple<ImageOptionsBase,string>(new BmpOptions
|
|
{
|
|
Compression = BitmapCompression.Rgb,
|
|
BitsPerPixel = 4,
|
|
Palette = ColorPaletteHelper.GetCloseImagePalette((RasterImage)image, 1 << 4)
|
|
},
|
|
"Rgb4.bmp")
|
|
},
|
|
{ new Tuple<ImageOptionsBase,string>(new BmpOptions
|
|
{
|
|
Compression = BitmapCompression.Rgb,
|
|
BitsPerPixel = 8,
|
|
Palette = ColorPaletteHelper.GetCloseImagePalette((RasterImage)image, 1 << 8)
|
|
}, "Rgb8.bmp")
|
|
}
|
|
};
|
|
}
|
|
case FileFormat.Cdr:
|
|
case FileFormat.Cmx:
|
|
case FileFormat.Odg:
|
|
case FileFormat.Otg:
|
|
case FileFormat.Eps:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(new SvgOptions()
|
|
{
|
|
Compress = true,
|
|
VectorRasterizationOptions = rasterizationOptionsFactory.Invoke()
|
|
},".svgz")
|
|
};
|
|
}
|
|
case FileFormat.Svg:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(new SvgOptions()
|
|
{
|
|
Compress = true
|
|
},".svgz")
|
|
};
|
|
}
|
|
case FileFormat.Dicom:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(new DicomOptions()
|
|
{
|
|
ColorType = ColorType.Grayscale8Bit, // or ColorType.Rgb24Bit for truecolor images
|
|
Compression = new Compression
|
|
{
|
|
Type = CompressionType.Jpeg,
|
|
Jpeg = new JpegOptions
|
|
{
|
|
Quality = 75
|
|
}
|
|
},
|
|
},null)
|
|
};
|
|
}
|
|
case FileFormat.Dng:
|
|
case FileFormat.Tiff:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(new TiffOptions(TiffExpectedFormat.Default)
|
|
{
|
|
Photometric = TiffPhotometrics.Ycbcr,
|
|
Compression = TiffCompressions.Jpeg,
|
|
BitsPerSample = new ushort[] { 8, 8, 8 }
|
|
}, null)
|
|
};
|
|
}
|
|
case FileFormat.Emf:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(
|
|
new EmfOptions()
|
|
{
|
|
Compress = true,
|
|
},
|
|
".emz")
|
|
};
|
|
}
|
|
case FileFormat.Wmf:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(
|
|
new WmfOptions()
|
|
{
|
|
Compress = true,
|
|
},
|
|
".wmz")
|
|
};
|
|
}
|
|
case FileFormat.Tga:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(
|
|
new PngOptions
|
|
{
|
|
CompressionLevel = 9,
|
|
Progressive = true,
|
|
ColorType = PngColorType.IndexedColor,
|
|
Palette = ColorPaletteHelper.GetCloseImagePalette((RasterImage)image, 1 << 5)
|
|
}, ".png")
|
|
};
|
|
}
|
|
case FileFormat.Gif:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(new GifOptions
|
|
{
|
|
IsPaletteSorted = false,
|
|
DoPaletteCorrection = false,
|
|
MaxDiff = 500,
|
|
ColorResolution = 7,
|
|
Palette = ColorPaletteHelper.GetCloseImagePalette((RasterImage)image, 1 << 8)
|
|
}, null)
|
|
};
|
|
}
|
|
case FileFormat.Ico:
|
|
{
|
|
IcoImage ico = image as IcoImage;
|
|
if (ico == null)
|
|
{
|
|
throw new NotSupportedException($"The ico image is expectecd but {fileFormat} is found");
|
|
}
|
|
|
|
var page = ico.Pages.OrderBy(p => p.Width * p.Height).ThenBy(p => p.BitsPerPixel).Last();
|
|
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(new IcoOptions
|
|
{
|
|
Format = Aspose.Imaging.FileFormat.Bmp,
|
|
BitsPerPixel = 8,
|
|
Palette = ColorPaletteHelper.GetCloseImagePalette(page as Aspose.Imaging.RasterImage, 1 << 8)
|
|
}, null)
|
|
};
|
|
}
|
|
case FileFormat.Jpeg2000:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(new Jpeg2000Options
|
|
{
|
|
Codec = Jpeg2000Codec.J2K,
|
|
Irreversible = false,
|
|
CompressionRatios = new[] { 100 }
|
|
}, null)
|
|
};
|
|
}
|
|
case FileFormat.Jpeg:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(new JpegOptions
|
|
{
|
|
CompressionType = JpegCompressionMode.Progressive,
|
|
ColorType = JpegCompressionColorMode.YCbCr,
|
|
Quality = 75
|
|
}, null)
|
|
};
|
|
}
|
|
case FileFormat.Png:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(new PngOptions
|
|
{
|
|
CompressionLevel = 9,
|
|
Progressive = true,
|
|
ColorType = PngColorType.IndexedColor,
|
|
Palette = ColorPaletteHelper.GetCloseImagePalette((RasterImage)image, 1 << 5)
|
|
}, null)
|
|
};
|
|
}
|
|
case FileFormat.Webp:
|
|
{
|
|
return new List<Tuple<ImageOptionsBase, string>>
|
|
{
|
|
new Tuple<ImageOptionsBase, string>(
|
|
new WebPOptions()
|
|
{
|
|
Lossless = false,
|
|
Quality = 50
|
|
}, null)
|
|
};
|
|
}
|
|
default:
|
|
{
|
|
throw new NotSupportedException($"FileFormat {fileFormat} is not supported");
|
|
}
|
|
}
|
|
}
|
|
|
|
Image doCompress(string outputFile, Image image, ImageOptionsBase compressOptions)
|
|
{
|
|
Image outImage = image;
|
|
|
|
outImage.Save(outputFile, compressOptions);
|
|
|
|
return outImage;
|
|
} |