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

Customizing Raster and Text Layers

One more example of Advanced PSD Add-on usage is rendering PSD files and customizing raster and text layers. Such functionality may be useful if you need to create several images based on the same template with different content, e.g. business cards, advertising booklets, postcards, etc. This article discusses the Real Estate Digest sample application which creates advertising booklets. It stores realty details in the internal database and uses this data to create booklets. The user can preview a booklet using the BitmapViewer or save it with the specified dpi as well as change some text or image information and choose a template. Note that the application provides a read-only access to real estate data and all your modifications will not affect sample database.

Real Estate Digest
Note

The full source code of this example can be found at Aurigma Forums:

http://forums.aurigma.com/yaf_postsm9066_Real-Estate-Digest-Demo.aspx

The main functionality of this application is implemented in the PsdRenderer class. Let us consider it more detailed.

PsdRenderer Class Overview

The PsdRenderer class is based on Advanced PSD Add-on. It is intended to render PSD files and customize raster and text layers. This class provides only one public method: Render(string, Dictionary<string, IReplacerInfo>, int) which returns the Bitmap class instance and accepts the following arguments:

  • The filename of the PSD template;
  • The dictionary which contains names of the layers we need to customize and new content for these layers;
  • The DPI of the result image.

The resulting bitmap represents an image where layers of the template are customized with new content and merged in correct way.

New content for raster and text layers is specified with BitmapReplacerInfo and TextReplacerInfo classes, respectively.

The TextReplacerInfo class specifies new text value. The BitmapReplacerInfo class specifies new bitmap to render and two additional placement parameters - ImageFitMode and ImageAlignment.

The ImageFitMode enumeration represents values specifying how to handle situations when a new image does not fit to size of original image. The following values are supported:

CropToFit Crops the image. The crop area is centered.
ResizeToFill Resizes the image so that it fills the whole placeholder, even if the resulting image is larger than the placeholder. The aspect ratio is preserved.
ResizeToFit Resizes the image to have the same size as a placeholder (preserving the aspect ratio though). The image is resized even if it is smaller than the placeholder.
ShrinkToFit Resizes the image to have the same size as a placeholder (preserving the aspect ratio though) only if the image is larger than the placeholder. If the image is smaller, it is not changed.

The ImageAlignment enumeration contains possible values how to aligh new image in the placeholder in the case when new image is smaller than the placeholder. It can be one of the values in the table below:

CenterBottom Aligns the image by the center horizontally and by the bottom vertically.
CenterCenter Aligns the image by the center horizontally and by the center vertically.
CenterTop Aligns the image by the center horizontally and by the top vertically.
LeftBottom Aligns the image by the left side horizontally and by the bottom vertically.
LeftCenter Aligns the image by the left side horizontally and by the center vertically.
LeftTop Aligns the image by the left side horizontally and by the top vertically.
RightBottom Aligns the image by the right side horizontally and by the bottom vertically.
RightCenter Aligns the image by the right side horizontally and by the center vertically.
RightTop Aligns the image by the right side horizontally and by the top vertically.

To gain a better understanding of the Render method let us consider a simple example. Assume that we have the PSD template which contains three layers: background raster layer which will not be replaced, image and text placeholders. Suppose, the layer contained an image placeholder has the "Image" name and the layer with a text placeholder has the "Text" name. To create both preview and printable versions of the personalized graphics using this template, the custom image, and text we need to perform the following steps:

  1. Create a dictionary.

    C#
    Dictionary<string, IReplacerInfo> replacers = new Dictionary<string,IReplacerInfo>();
    
  2. Fill this dictionary with two replacers: one for the image layer and another for the text

    C#
    Aurigma.GraphicsMill.Bitmap image = new Aurigma.GraphicsMill.Bitmap(@"C:\image.jpg");
    replacers.Add("Image", new BitmapReplacerInfo(image, ImageFitMode.ResizeToFit, ImageAlignment.LeftCenter));
    replacers.Add("Text", new TextReplacerInfo("New text"));
    
  3. Create new instance on the PsdRenderer class and use its method Render to create a preview version with 72 dpi and printable one with 300 dpi.

    C#
    PsdRenderer rend = new PsdRenderer();
    Aurigma.GraphicsMill.Bitmap print = rend.Render(template, replacers, 300);
    print.Save(@"C:\print.jpg");
    Aurigma.GraphicsMill.Bitmap preview = rend.Render(template, replacers, 72);
    preview.Save(@"C:\preview.jpg");