This documentation is for the old version. Go to the latest Graphics Mill docs

Creating Personalized Graphics

It is often necessary to create several images which differ only in minor details. For example, it is widely used when it is necessary to get an image based on a template with placeholder for data. Similar approach is used in Microsoft Office for the Mail Merge function: the user prepares a template with placeholders and link it with the data source.

Advanced PSD Add-on for Graphics Mill is ideal for this task. You can prepare PSD template of the necessary graphics in Adobe® Photoshop®, process it with the Advanced PSD Add-on, and generate personalized images.

In this article we will create a simple application which generates a set of ID cards based on data from standard Northwind database which is shipped with MSSQL.

Preparing PSD Template

First of all, let's create a PSD template of the ID card in Photoshop®.

Warning

Do not forget that Advanced PSD Add-on supports only limited subset of PSD format. That's why to merge PSD layers into an image precisely, you should simplify the template as much as possible. In particular, you should not use masks, different blending options, vector paths, multiple alpha channels, etc.

We will personalize three elements of the ID card: person name, title, and photo. Add placeholder frames and define their names. You will get the similar template:

PSD template for ID cards.

It will contain the following layers:

Screenshot of list of ID card template layers.

Merging Layers with Data Pulled from Data Source

To generate ID cards, we need to make query to database, get persons' name, title, and photo, and insert them into placeholders when rasterizing the PSD file. For rasterization we will reuse the RasterizePsd method described in the Merging Layers topic.

Visual Basic
Dim connection As New System.Data.SqlClient.SqlConnection(connectionString)

Dim employees As New System.Data.SqlClient.SqlCommand( _
 "SELECT FirstName, LastName, Title, Photo FROM Employees", connection)

connection.Open()

Dim i As Integer = 1

Try
    Dim reader As System.Data.SqlClient.SqlDataReader = employees.ExecuteReader
    While reader.Read


        ' Create collection of layers which should be inserted in placeholders.
        Dim updatedLayers As New System.Collections.Hashtable

        ' Add text layers - full name and title. 
        updatedLayers.Add("FullName", Convert.ToString(reader("FirstName")) & " " & Convert.ToString(reader("LastName")))
        updatedLayers.Add("Title", Convert.ToString(reader("Title")))

        Dim data As Byte() = reader("Photo")
        Dim stream As New System.IO.MemoryStream

        ' Northwind database stores images not as raw files, but as OLE object.
        ' It means that the field stores not only the file but also OLE header.
        ' This header occupies 78 bytes. To load the image to Graphics Mill, it
        ' is necessary to skip this header.
        '
        ' Note, this is specific only to Northwind database. Most likely your 
        ' database stores the image file only without any OLE headers. In this 
        ' case just set offset to 0.
        Dim offset As Integer = 78
        stream.Write(data, offset, data.Length - offset)

        ' Load the photo and add it to the Photo placeholder.
        Dim photo As New Aurigma.GraphicsMill.Bitmap(stream)
        updatedLayers.Add("Photo", photo)

        ' Call RasterizePsd method described in the Merging Layers topic.
        Dim bitmap As Aurigma.GraphicsMill.Bitmap = RasterizePsd(templateFilename, updatedLayers)

        ' Save result to disk.
        bitmap.Save("c:\card_" & i & ".jpg")

        i += 1

    End While
Finally
    connection.Close()
End Try

C#
System.Data.SqlClient.SqlConnection connection = 
    new System.Data.SqlClient.SqlConnection(connectionString);

System.Data.SqlClient.SqlCommand employees = new System.Data.SqlClient.SqlCommand(
    "SELECT FirstName, LastName, Title, Photo FROM Employees", connection);

connection.Open();

int i = 1;

try
{
    System.Data.SqlClient.SqlDataReader reader = employees.ExecuteReader();
    while (reader.Read())
    {
        // Create collection of layers which should be inserted in placeholders.
        System.Collections.Hashtable updatedLayers = new System.Collections.Hashtable();

        // Add text layers - full name and title. 
        updatedLayers.Add("FullName", Convert.ToString(reader["FirstName"]) + " " + Convert.ToString(reader["LastName"]));
        updatedLayers.Add("Title", Convert.ToString(reader["Title"]));

        byte[] data = (byte[])(reader["Photo"]);
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        
        // Northwind database stores images not as raw files, but as OLE object.
        // It means that the field stores not only the file but also OLE header.
        // This header occupies 78 bytes. To load the image to Graphics Mill, it
        // is necessary to skip this header.
        //
        // Note, this is specific only to Northwind database. Most likely your 
        // database stores the image file only without any OLE headers. In this 
        // case just set offset to 0.
        int offset = 78;
        stream.Write(data, offset, data.Length - offset);

        // Load the photo and add it to the Photo placeholder.
        Aurigma.GraphicsMill.Bitmap photo = new Aurigma.GraphicsMill.Bitmap(stream);
        updatedLayers.Add("Photo", photo);

        // Call RasterizePsd method described in the Merging Layers topic.
        Aurigma.GraphicsMill.Bitmap bitmap = RasterizePsd(templateFilename, updatedLayers);

        // Save result to disk.
        bitmap.Save(@"c:\card_" + i + ".jpg");
        
        i++;
    }
}
finally
{
    connection.Close();
}

After you run this code, you will get the similar ID card:

Result ID card.
Note

You may find that the photo on the ID card has too bad quality. This problem is not caused by Graphics Mill. It happens because photos is the Northwind database are stored as GIF files of low resolution. If you use this code for database which stores high-quality photos, ID cards will be fine as well.