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.
Graphics Mill supports two different ways to load and save raster images:
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:
The following sections examine both approaches in more detail.
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));
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:
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:
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"); } } }
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:
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:
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); } } }
To load vector images, Graphics Mill provides the following classes:
To load an image from a file, you should do the following:
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().
To save vector images, Graphics Mill provides the following classes:
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 draw a graphics container 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:
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.