---
title: "Working with Files"
canonical: https://www.graphicsmill.com/docs/gm/working-with-files.htm
product: "Graphics Mill"
---

# Working with Files
 
Usually, images are stored in files. Using Graphics Mill you can process [various image file formats](https://www.graphicsmill.com/docs/gm/supported-file-formats.htm.md) 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](https://www.graphicsmill.com/docs/gm/O_T_Aurigma_GraphicsMill_Bitmap_ctor.htm.md) and [Bitmap.Save](https://www.graphicsmill.com/docs/gm/O_T_Aurigma_GraphicsMill_Bitmap_Save.htm.md) 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](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Bitmap.htm.md) class, you can use the **Bitmap** class [constructor](https://www.graphicsmill.com/docs/gm/O_T_Aurigma_GraphicsMill_Bitmap_ctor.htm.md), as follows:
 
```csharp
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](https://www.graphicsmill.com/docs/gm/supported-file-formats.htm.md) image. If you set the `out.tif` filename, an image is saved as [TIFF](https://www.graphicsmill.com/docs/gm/supported-file-formats.htm.md) format.
 
```csharp
bitmap.Save(@"Images\Output\out.jpg");
```
 
If you want to control output parameters, such as compression type, quality, etc., you can use the [WriterSettings](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_WriterSettings.htm.md) 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](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PngSettings.htm.md) instance as a writer settings. Here is a code snippet to save an image in JPEG format with quality 70:
 
```csharp
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](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_BmpReader.htm.md)
- [EpsReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_EpsReader.htm.md)
- [GifReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_GifReader.htm.md)
- [JpegReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_JpegReader.htm.md)
- [PdfReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfReader.htm.md)
- [PngReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PngReader.htm.md)
- [PsdReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_Psd_PsdReader.htm.md)
- [SvgReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_SvgReader.htm.md)
- [TgaReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_TgaReader.htm.md)
- [TiffReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_TiffReader.htm.md)
- [WebPReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_WebPReader.htm.md)

All these classes are derived from the [ImageReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ImageReader.htm.md) 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](https://www.graphicsmill.com/docs/gm/O_T_Aurigma_GraphicsMill_Codecs_ImageReader_Create.htm.md) 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:
 
1. 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.
2. Get the needed frame from the [Frames](https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_Frames.htm.md) collection by specifying a frame number. To get a total number of frames stored in the file, use the standard [Count](https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_FrameCollectionBase_Count.htm.md) property.
3. To get a bitmap use the [GetBitmap()](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_Frame_GetBitmap.htm.md) method. Other [Frame properties and methods](https://www.graphicsmill.com/docs/gm/AllMembers_T_Aurigma_GraphicsMill_Codecs_Frame.htm.md) 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](https://www.graphicsmill.com/docs/gm/supported-file-formats.htm.md) files:
 
```csharp
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](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_BmpWriter.htm.md)
- [EpsWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_EpsWriter.htm.md)
- [GifWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_GifWriter.htm.md)
- [JpegWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_JpegWriter.htm.md)
- [PdfWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfWriter.htm.md)
- [PngWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PngWriter.htm.md)
- [SvgWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_SvgWriter.htm.md)
- [TgaWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_TgaWriter.htm.md)
- [TiffWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_TiffWriter.htm.md)
- [WebPWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_WebPWriter.htm.md)

All writers are descendants of the [ImageWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ImageWriter.htm.md) class. So, if you do not know the type of a file you are going to save, use the [Create](https://www.graphicsmill.com/docs/gm/O_T_Aurigma_GraphicsMill_Codecs_ImageWriter_Create.htm.md) 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:
 
1. Create a writer instance of the necessary format and pass a filename as a constructor argument.
2. Create a [Frame](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_Frame.htm.md) instance or get it from a reader.
3. Run a pipe by calling the [Pipeline.Run](https://www.graphicsmill.com/docs/gm/O_T_Aurigma_GraphicsMill_Pipeline_Run.htm.md) method. If you are not familiar with pipelines, read the [Understanding Image Processing Approaches in Graphics Mill](https://www.graphicsmill.com/docs/gm/understanding-image-processing-approaches-in-graphics-mill.htm.md) topic.
4. 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:
 
```csharp
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:
 
1. Create a reader instance of the necessary type, and pass a filename as a constructor argument.
2. Get the needed frame from the **Frames** collection by specifying a frame number.
3. Get a [GraphicsContainer](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_GraphicsContainer.htm.md) of the frame.
4. 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](https://www.graphicsmill.com/docs/gm/graphics-container.htm.md). 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:
 
```csharp
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:
 
```csharp
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](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Pipeline.htm.md) 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()](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfFrame_GetContent.htm.md).
 
### 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)](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawContainer_3_098aa7ce.htm.md) obtained by the **PdfFrame.GetContent()** method on the **writer** object:
 
```csharp
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:
 
1. Create a writer instance of the necessary format and pass a filename as a constructor argument.
2. Add a page to the writer to be able to write in a PDF file (you skip this step for single page SVG files).
3. Get a **GraphicsContainer** of the added page (or the graphics container of the writer in the case of SVG files).
4. Write anything you need in the container.
5. 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.
 
```csharp
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](https://www.graphicsmill.com/docs/gm/working-with-pdf.htm.md) topic for more details on **PdfWriter**.
 
## See Also
  
### Reference
 
- [GraphicsMill.Bitmap Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Bitmap.htm.md)
- [Codecs.ImageReader Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ImageReader.htm.md)
- [Codecs.ImageWriter Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ImageWriter.htm.md)
- [Codecs.Frame Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_Frame.htm.md)
- [GraphicsMill.Pipeline Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Pipeline.htm.md)

### Manual
 
- [Supported File Formats](https://www.graphicsmill.com/docs/gm/supported-file-formats.htm.md)
- [Understanding Image Processing Approaches in Graphics Mill](https://www.graphicsmill.com/docs/gm/understanding-image-processing-approaches-in-graphics-mill.htm.md)
- [Manipulating Vector Images with Graphics Containers](https://www.graphicsmill.com/docs/gm/graphics-container.htm.md)
