- About Graphics Mill
- What's New?
- Breaking Changes in Graphics Mill 12
- Breaking Changes in Graphics Mill 11
- Features
- Supported File Formats
- System Requirements
- Getting Started
- Working with Image Formats
- Transforming Images (Resize, Rotate, Crop)
- Working with Metadata
- Color Management and Color Reduction
- Drawing Text and Graphics
- Color Keying (Green Screen Removal)
- Channels and Pixel Data
- Color and Tone Correction
- Working with Photoshop (PSD) Files
- Image Filters
- Processing Large Images
- Basics of Image Processing
- Troubleshooting
- Class Library Reference
- Licensing
- End User License Agreement
- Copyright Notices
Working with Files
Usually, images are stored in files. Using Graphics Mill you can process various image file formats of both raster and vector images. This article explains how you can load and save images from/to files.
Working with Raster Images
Graphics Mill supports two different ways to load and save raster images:
- using constructors and Bitmap.Save methods
- using image readers and image writers
The first approach is the simplest one, but it can only be used in limited cases, specifically when you do not have to load multiframe or large images. The second method, using image readers and writers, offers you all the SDK capabilities:
- multiframe images support (such as multipage TIFF files, animated GIFs, etc.)
- memory-friendly image processing
- support of metadata (EXIF, IPTC, XMP, and Adobe Resources)
The following sections examine both approaches in more detail.
Using Bitmap Class
To load an image from a file using the Bitmap class, you can use the Bitmap class constructor, as follows:
var bitmap = new Bitmap(@"Images\in.jpg");
In order to save an image, pass a target filename as an argument to the Save method. Graphics Mill analyzes the file extension, and if the extension is recognized, the image is saved in the appropriate file format. For example, if you specify the out.jpg filename, an image is saved as JPEG image. If you set the out.tif filename, an image is saved as TIFF format.
bitmap.Save(@"Images\Output\out.jpg");
If you want to control output parameters, such as compression type, quality, etc., you can use the WriterSettings class descendants. In this case, you should remember that WriterSettings should match a file extension. For example, if you use the out.png filename, you should pass the PngSettings instance as a writer settings. Here is a code snippet to save an image in JPEG format with quality 70:
bitmap.Save(@"Images\Output\out.jpg", new JpegSettings(70));
Using Image Readers
To read data from a file, Graphics Mill provides a set of classes called image readers. Each supported file type is presented by a reader class. The following readers are available:
- BmpReader
- EpsReader
- GifReader
- JpegReader
- PdfReader
- PngReader
- PsdReader
- SvgReader
- TgaReader
- TiffReader
- WebPReader
All these classes are derived from the ImageReader class. So, if you do not know what image format you are loading images from, it is best to use the ImageReader class. Its constructor Create can accept a filename as an argument, and returns a reader instance.
Graphics Mill allows reading both raster and vector files in the PDF and SVG formats. Later in this topic, we will describe how you can work with vector graphics.
To load an image from a file (that is to get a Bitmap instance which contains the image), you should do the following:
- Create a reader instance of the necessary type, and pass a filename as a constructor argument. For example, if you load a JPEG file, you should create an instance of the JpegReader class.
- Get the needed frame from the Frames collection by specifying a frame number. To get a total number of frames stored in the file, use the standard Count property.
- To get a bitmap use the GetBitmap() method. Other Frame properties and methods allow getting additional details like width, height, etc. without loading an entire bitmap into memory.
The following code snippet demonstrates how to split a multiframe image file into separate JPEG files:
using (var reader = new TiffReader(@"Images\in.tif"))
{
for (int i = 0; i < reader.Frames.Count; i++)
{
using (var bitmap = reader.Frames[i].GetBitmap())
{
bitmap.Save(@"Images\Output\frame_" + i.ToString() + ".jpg");
}
}
}
Using Image Writers
Image writers are very similar to the Image readers discussed previously. Image writers are a set of classes designed to write image data to files of a supported format. The following writers are available:
- BmpWriter
- EpsWriter
- GifWriter
- JpegWriter
- PdfWriter
- PngWriter
- SvgWriter
- TgaWriter
- TiffWriter
- WebPWriter
All writers are descendants of the ImageWriter class. So, if you do not know the type of a file you are going to save, use the Create constructor, which can accept a filename as an argument, and returns a writer instance.
Graphics Mill allows writing both raster and vector files in the PDF and SVG formats. Later in this topic, we will describe how you can work with vector graphics.
Usually to save an image file you should perform the following steps:
- Create a writer instance of the necessary format and pass a filename as a constructor argument.
- Create a Frame instance or get it from a reader.
- Run a pipe by calling the Pipeline.Run method. If you are not familiar with pipelines, read the Understanding Image Processing Approaches in Graphics Mill topic.
- If you need to write several frames, repeat the two previous steps as many times as necessary.
The following code creates an animated GIF file by grouping JPEG images:
string dir = @"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\\";
string[] images = { "Chrysanthemum.jpg", "Desert.jpg", "Jellyfish.jpg", "Tulips.jpg" };
using (var writer = new GifWriter(@"Images\Output\Slideshow.gif"))
using (var cc = new ColorConverter(PixelFormat.Format8bppIndexed))
{
writer.FrameOptions.Delay = 100;
foreach (string image in images)
{
using (var reader = new JpegReader(dir + image))
{
Pipeline.Run(reader + cc + writer);
}
}
}
Working with Vector Images
Using Image Readers
To load vector images, Graphics Mill provides the following classes:
- PdfReader
- SvgReader
To load an image from a file, you should do the following:
- Create a reader instance of the necessary type, and pass a filename as a constructor argument.
- Get the needed frame from the Frames collection by specifying a frame number.
- Get a GraphicsContainer of the frame.
- Process the container as a raster or vector graphics.
PdfReader and SvgReader are derived from the ImageReader class. So, you can create a reader instance through the Create method as well as through constructors of these readers.
To work with vector graphics, you should use graphics containers. These containers allow you to process vector graphics without rasterization. PdfReader creates a graphics container for a single page of a multipage document. The following example shows how you can load a PDF file and get a graphics container with the first page content:
using (var reader = new PdfReader(@"Images\in.pdf", 90, 90))
using (var container = reader.Frames[0].GetContent())
{
//...
}
The following example shows how you can rasterize a PDF file:
var dpi = 90f;
var quality = 90;
using (var reader = new PdfReader(@"Images\in.pdf", dpi, dpi))
using (var writer = new JpegWriter(@"Images\Output\out.jpg", quality, false, true))
{
Pipeline.Run(reader + writer);
}
Here, you create the reader instance for the in.pdf file and run the Pipeline to rasterize the PDF document and save it as JPEG. In this case, implicit rasterization occurs in the pipeline.
Also, to rasterize a PDF page, you can call the Frame.GetBitmap() method instead of PdfFrame.GetContent().
Using Image Writers
To save vector images, Graphics Mill provides the following classes:
- PdfWriter
- SvgWriter
- EpsWriter
These writers allow you to create documents containing raster images, vector shapes, and vector text. Let us look at the simplest use case scenario when you need to convert PDF to SVG. Here, you assign the reader and writer objects to set up the source and destination files. Then, you DrawContainer(GraphicsContainer, Single, Single) obtained by the PdfFrame.GetContent() method on the writer object:
using (var reader = new PdfReader(@"Images\in.pdf", dpi, dpi))
using (var container = reader.Frames[0].GetContent())
using (var writer = new SvgWriter(@"Images\Output\out.svg", reader.Width, reader.Height))
using (var graphics = writer.GetGraphics())
{
graphics.DrawContainer(container, 0, 0);
}
Usually to save an image file you should perform the following steps:
- Create a writer instance of the necessary format and pass a filename as a constructor argument.
- Add a page to the writer to be able to write in a PDF file (you skip this step for single page SVG files).
- Get a GraphicsContainer of the added page (or the graphics container of the writer in the case of SVG files).
- Write anything you need in the container.
- If you need to write several pages, repeat the three previous steps as many times as necessary.
The following example shows how you can write vector text and graphical primitives in an output PDF file.
var dpi = 90f;
using (var container = new GraphicsContainer(350, 300, dpi, dpi))
using (var graphics = container.GetGraphics())
{
graphics.DrawLine(new Pen(RgbColor.Red, 3), 50, 5, 200, 270);
graphics.DrawEllipse(new Pen(RgbColor.Green, 10), new System.Drawing.Rectangle(50, 50, 250, 200));
using (var writer = new PdfWriter(@"Images\Output\out.pdf"))
{
writer.AddPage(350, 300, dpi, dpi);
using (var pdfGraphics = writer.GetGraphics())
{
pdfGraphics.DrawContainer(container, 0, 0);
pdfGraphics.DrawText(new PlainText("Graphics Mill", FontRegistry.Installed.CreateFont("Arial", 16, dpi, dpi), 5, 20));
}
}
}
Refer to the Working with PDF topic for more details on PdfWriter.
See Also
Reference
- GraphicsMill.Bitmap Class
- Codecs.ImageReader Class
- Codecs.ImageWriter Class
- Codecs.Frame Class
- GraphicsMill.Pipeline Class