CompressedImage is an image type that stores raster data in compressed form instead of fully unpacking it into memory.
This feature was introduced primarily to increase the efficiency of getting PDF content in the form of a GraphicsContainer. Previously, images were represented only as Bitmap, which always keeps raster data decompressed in memory. Now both Bitmap and CompressedImage derive from the common base class Image:
This approach is especially useful when working with PDF content.
In most cases, you do not need to create or read CompressedImage objects manually.
The main scenario is PDF processing. When Graphics Mill reads PDF content, it can keep image data as compressed streams whenever possible. These streams remain compressed until you explicitly rasterize them.
This provides two important benefits:
If you save such content back to PDF, the original compressed stream can be reused as-is.
Manual creation of a CompressedImage is an advanced scenario.
Typically, you only need it when you want more control over how images are compressed in generated PDF files.
CompressedImage exposes two separate streams:
This matches the internal representation used by PDF images, where color and alpha data may be stored separately. In general, these streams may even have different dimensions.
However, if you create a CompressedImage manually, the color and alpha parts always correspond to the same image dimensions.
Use this approach when you want to build a compressed image from an image source and explicitly choose the compression type.
using (var reader = ImageReader.Create(@"Images\in.jpg"))
{
var pipeline = new Pipeline();
pipeline.Add(reader);
using (var compressedImage = CompressedImage.CreateFromPipeline(pipeline, CompressionType.Zip))
{
Console.WriteLine("Image width: " + compressedImage.Width);
Console.WriteLine("Image height: " + compressedImage.Height);
Console.WriteLine("Compression: " + compressedImage.ColorStream.Compression);
Console.WriteLine("Stream size: " + compressedImage.ColorStream.Size);
}
}
You can also create a compressed image directly from a file:
using (var compressedImage = CompressedImage.CreateFromFile(@"Images\in.jpg", CompressionType.Zip))
{
Console.WriteLine("Image width: " + compressedImage.Width);
Console.WriteLine("Image height: " + compressedImage.Height);
Console.WriteLine("Compression: " + compressedImage.ColorStream.Compression);
Console.WriteLine("Stream size: " + compressedImage.ColorStream.Size);
using (var bitmap = compressedImage.GetBitmap())
{
bitmap.Transforms.Resize(500, 500, ResizeInterpolationMode.High, ResizeMode.Fit);
bitmap.Save(@"Images\Output\out.jpg");
}
}
A CompressedImage is intended mainly for inspection and deferred rasterization.
You can:
This can be useful in preflight and optimization scenarios.
CompressedImage does not provide direct pixel-level editing. If you need to access or modify pixels, convert it to a Bitmap first:
using (var compressedImage = CompressedImage.CreateFromFile(@"Images\in.jpg", CompressionType.Zip))
using (var bitmap = compressedImage.GetBitmap())
{
bitmap.Transforms.Resize(500, 500, ResizeInterpolationMode.High, ResizeMode.Fit);
bitmap.Save(@"Images\Output\out.jpg");
}