---
title: "Manipulating Vector Images with Graphics Containers"
canonical: https://www.graphicsmill.com/docs/gm/graphics-container.htm
product: "Graphics Mill"
---

# Manipulating Vector Images with Graphics Containers
 
Graphics Mill allows you to use containers for vector graphics. This topic describes how you can apply these containers.
 
## Creating a GraphicsContainer
  
The [GraphicsContainer](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_GraphicsContainer.htm.md) class provides an easy way to manipulate vector graphics. It presents storage that can include vector images, bitmaps, texts, and other graphics containers. You can draw such containers on bitmaps or combine them with each other without rasterization.
 
To start working with vector graphics, you can either create a graphics container from scratch or get it from PSD, PDF, or SVG files. After that, you can draw on it via [Graphics](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_Graphics.htm.md) returned by the [GraphicsContainer.GetGraphics()](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_GraphicsContainer_GetGraphics.htm.md) method. There are three ways to create a graphics container:
 
- From scratch
- From a PSD layer
- From a PDF or SVG page

### Create a GraphicsContainer from Scratch
 
The following example shows how you can create, draw, and rasterize such a container.
 
```csharp
var dpi = 72;

using (var container = new GraphicsContainer(300, 300, dpi, dpi))
using (var graphics = container.GetGraphics())
{
    graphics.DrawLine(new Pen(RgbColor.Red, 3), 100, 100, 150, 250);
    graphics.DrawEllipse(new Pen(RgbColor.Green, 10), new System.Drawing.Rectangle(50, 50, 250, 200));

    using (var bitmap = new Bitmap(400, 400, PixelFormat.Format32bppArgb))
    using (var bitmapGraphics = bitmap.GetGraphics())
    {
        // Draw the container on the bitmap twice.
        bitmapGraphics.DrawContainer(container, 0, 0);
        bitmapGraphics.DrawContainer(container, 50, 50);

        bitmap.Save(@"Images\Output\out.png");
    }
}
```
 
When you draw a GraphicsContainer on a bitmap by the [DrawContainer(GraphicsContainer, Single, Single)](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawContainer_3_098aa7ce.htm.md) method, it is rasterized, and you can save it as a raster file.
 
### Convert a PSD Layer
 
You can use another way to create a GraphicsContainer if you work with PSD files. In this case, the container can be created by the [PsdFrameExtensions.ToGraphicsContainer(PsdFrame)](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Templates_PsdFrameExtensions_ToGraphicsContainer_1_b128deba.htm.md) static method. A [PSD frame](https://www.graphicsmill.com/docs/gm/working-with-psd-files.htm.md) represents a layer of a PSD file in Graphics Mill. This method allows you to convert a frame to a new GraphicsContainer.
 
```csharp
using (var reader = new PsdReader(@"Images\BusinessCard.psd"))
// Get the first PSD layer, for example.
using (var frame = reader.Frames[0])
{
    if (frame.Type != FrameType.Unknown && frame.Type != FrameType.Group)
    {
        // Put the layer into the container.
        using (var container = PsdFrameExtensions.ToGraphicsContainer(frame))
        using (var bitmap = new Bitmap(frame.Width, frame.Height, PixelFormat.Format32bppArgb, RgbColor.White))
        using (var graphics = bitmap.GetGraphics())
        {
            // Draw the container on the bitmap.
            graphics.DrawContainer(container, 0, 0);
            bitmap.Save(@"Images\Output\out.png");
        }
    }
}
```
 
### Get Content of a PDF or SVG File
 
Also, [PdfReader](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfReader.htm.md) and **SvgReader** can create a graphics container for a frame that they have read. In the case of PDFs, these frames represent pages of multipage documents. Let us look at how you can retrieve vector graphics from a PDF file using the **PdfReader** class.
 
```csharp
using (var reader = new PdfReader(@"Images\in.pdf", dpi, dpi))
// Get the first page of the PDF file, for example:
using (var frame = reader.Frames[0])
using (var container = frame.GetContent())
using (var bitmap = new Bitmap(frame.Width, frame.Height, PixelFormat.Format32bppArgb, RgbColor.White))
using (var graphics = bitmap.GetGraphics())
{
    // Draw the container on the bitmap.
    graphics.DrawContainer(container, 0, 0);
    bitmap.Save(@"Images\Output\out.jpg");
}
```
 
Here, the [PdfFrame.GetContent()](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfFrame_GetContent.htm.md) method returns the GraphicsContainer object that is placed into the container.
 
## Transforming a Graphics Container
  
Graphics Mill applies transformations to raster and vector images. To transform the content of a graphics container, you can rasterize it through [ImageGenerator](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_ImageGenerator.htm.md) first. The following example shows how you can read graphics from a PDF file, get vector objects through **PdfFrame.GetContent()**, and then place them into a graphics container via the **Graphics.DrawContainer(GraphicsContainer, Single, Single)** method. The **ImageGenerator** class converts the graphics container into a bitmap that can be used for the transformation later. Here, you run a [pipeline](https://www.graphicsmill.com/docs/gm/transforming-images-with-pipelines.htm.md) to resize the image.
 
```csharp
var dpi = 150f;
using (var reader = new PdfReader(@"Images\in.pdf", dpi, dpi))
// Get the first page of the PDF file, for example:
using (var frame = reader.Frames[0])
using (var frameContent = frame.GetContent())
using (var container = new GraphicsContainer(frame.Width, frame.Height, frame.DpiX, frame.DpiY))
using (var graphics = container.GetGraphics())
{
    // Draw vector graphics in the container.
    graphics.DrawContainer(frameContent, 0, 0);

    // Rasterize the container.
    using (var image = new ImageGenerator(container, PixelFormat.Format24bppRgb, RgbColor.Transparent))
    using (var resize = new Resize(100, 0))
    using (var writer = ImageWriter.Create(@"Images\Output\out.png"))
    {
       Pipeline.Run(image + resize + writer);
    }
}
```

The following example shows transformations of vector images without rasterization. It illustrates how you can downsize the image twice as much as it originally was and then rotate it using the coordinate transformation.
 
```csharp
using (var reader = new PdfReader(@"Images\in.pdf", dpi, dpi))
using (var frame = reader.Frames[0])
using (var container = frame.GetContent())
using (var writer = new PdfWriter(@"Images\Output\out.pdf"))
{
    var resizeK = 0.5f;

    writer.AddPage((int)(frame.Height * resizeK), (int)(frame.Width * resizeK),
        dpi, dpi, RgbColor.White, null);

    using (var graphics = writer.GetGraphics())
    {
        graphics.Transform.Scale(resizeK, resizeK);
        graphics.Transform.Rotate(90f);
        graphics.Transform.Translate(0, -frame.Height);

        graphics.DrawContainer(container, 0, 0);
    }
}
```
 
## Working with Files
  
Graphics Mill supports three file formats of vector graphics. These are [PDF](https://www.graphicsmill.com/docs/gm/working-with-pdf.htm.md), [SVG](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_SvgWriter.htm.md), and [EPS](https://www.graphicsmill.com/docs/gm/working-with-eps.htm.md). You can read from PDF and SVG using the **PdfReader** and **SvgReader** classes. Also, you can write to PDF, SVG, and EPS using the [PdfWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfWriter.htm.md), **SvgWriter**, and [EpsWriter](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_EpsWriter.htm.md) classes, correspondingly.
 
The previous examples in this topic show how you can apply PdfReader and PdfWriter.
 
Saving vector graphics into an SVG file is completely the same as saving into EPS. So, let us illustrate how you can do it using **SvgWriter**. In the following example, you convert a PDF to an SVG file and draw some watermark text on it.
 
```csharp
var dpi = 72;
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", 400, 400, dpi, dpi))
using (var graphics = writer.GetGraphics())
{
    graphics.DrawContainer(container, 0, 0);
    graphics.DrawText(new PlainText("Graphics Mill", FontRegistry.Installed.CreateFont("Arial", 12, dpi, dpi), 50, 50));
}
```
 
Now, open the `out.svg` file in an editor, in Inkscape for example, and verify that the file contains **path** elements for text objects and graphical primitives, and an **image** element for raster images.
 
![The saved SVG file in Inkscape.](https://www.graphicsmill.com/docs/gm/GraphicsContainerSVG.png)
 
## Color Management
  
Graphics containers may include elements in different color spaces. For example, there may be a bitmap image in the CMYK color space and graphical primitives in RGB. So, which color space should be used to rasterize the container?
 
Graphics Mill provides the [GraphicsContainer.GetPreferredPixelFormat()](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_GraphicsContainer_GetPreferredPixelFormat.htm.md) and [GraphicsContainer.GetPreferredColorProfile(ColorSpace)](https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_GraphicsContainer_GetPreferredColorProfile_1_9e5a71e1.htm.md) methods to resolve this problem. The first one gets the most preferred [PixelFormat](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_PixelFormat.htm.md) for elements in the container, and the second method returns [ColorProfile](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_ColorProfile.htm.md) for given pixel format. The following example shows how you can apply these methods.
 
```csharp
using (var reader = new PdfReader(@"Images\in.pdf", dpi, dpi))
using (var frame = reader.Frames[0])
using (var content = frame.GetContent())
using (var bitmap = new Bitmap(frame.Width, frame.Height, content.GetPreferredPixelFormat()
    , RgbColor.White))
{
    bitmap.ColorProfile = content.GetPreferredColorProfile(bitmap.PixelFormat.ColorSpace);

    using (var graphics = bitmap.GetGraphics())
    {
        graphics.DrawContainer(content, 0f, 0f);
    }

    bitmap.Save(@"Images\Output\out.tif");
}
```
 
## See Also
  
### Reference
 
- [GraphicsContainer Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_GraphicsContainer.htm.md)
- [PdfWriter Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfWriter.htm.md)
- [PdfReader Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfReader.htm.md)
- [PsdReader Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_Psd_PsdReader.htm.md)
- [SvgWriter Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_SvgWriter.htm.md)
- [SvgReader Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_SvgReader.htm.md)
- [EpsWriter Class](https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_EpsWriter.htm.md)

### Manual
 
- [Working with Files](https://www.graphicsmill.com/docs/gm/working-with-files.htm.md)
- [Working with Photoshop (PSD) Files](https://www.graphicsmill.com/docs/gm/working-with-psd-files.htm.md)
- [Transforming Images with Pipelines](https://www.graphicsmill.com/docs/gm/transforming-images-with-pipelines.htm.md)
- [Working with PDF](https://www.graphicsmill.com/docs/gm/working-with-pdf.htm.md)
