---
title: "Replace Background"
canonical: https://www.graphicsmill.com/samples/PSDFormat/RenderTemplates/ReplaceBackground_cs
product: "Graphics Mill"
source: "PSDFormat/RenderTemplates/ReplaceBackground.cs"
---

# Replace Background

Replaces background image of PSD image and saves to PDF file.

## Instruction

Write a C# program that replaces the background raster layer in "BusinessCard.psd" by processing each frame with custom FrameCallback, replacing raster frames with "Venice.jpg" loaded via ImageReader using ToGraphicsContainer with ResizeMode.ImageFill to fit the background image into the PSD layer bounds while preserving non-raster layers (text, vectors), then renders the modified PSD composition to PDF "ReplaceBackground.pdf" using PsdProcessor.Render.

## Usings

```csharp
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Codecs.Psd;
using Aurigma.GraphicsMill.Templates;
using Aurigma.GraphicsMill.Transforms;
```

## Code Snippet

```csharp
PsdProcessor psdProcessor = new PsdProcessor();

var psName = psdProcessor.FontResolver.FontRegistry.Add("ARIALUNI.TTF")[0].PostscriptName;
psdProcessor.FontResolver.FontRegistry.FallbackFonts.Add(psName);

psdProcessor.FrameCallback = (processor, frame) =>
{
    if (frame.Type != FrameType.Raster)
    {
        return processor.ProcessFrame(frame);
    }

    using (var background = ImageReader.Create("Venice.jpg"))
    {
        background.CloseOnDispose = false;

        return frame.ToGraphicsContainer(background, ResizeMode.ImageFill);
    }
};

psdProcessor.Render("BusinessCard.psd", "ReplaceBackground.pdf");
```

## Explanation

Below is a C# code sample that replaces PSD background with external image:

The code demonstrates dynamic PSD background replacement using FrameCallback to intercept raster frame processing, substituting original background content with external "Venice.jpg" via ImageReader and ToGraphicsContainer with ImageFill mode for proper aspect ratio handling within PSD layer bounds. Non-raster elements (text, vector shapes) pass through unchanged via processor.ProcessFrame, enabling template-based design workflows where backgrounds can be swapped while maintaining foreground content positioning, typography, and vector elements in the final PDF output.

## Tags

- [Personalization](<https://www.graphicsmill.com/samples/tags/personalization_md>): Replacing placeholders with dynamic content
- [PSD Processor](<https://www.graphicsmill.com/samples/tags/psd-processor_md>): Automating PSD-based personalization

## Input

- [Venice.jpg](<https://www.graphicsmill.com/samples/PSDFormat/compiled/Debug/Venice_jpg>) (preview)
- [BusinessCard.psd](<https://www.graphicsmill.com/samples/PSDFormat/compiled/Debug/BusinessCard_psd>) (download)

## Output

- [ReplaceBackground.pdf](<https://www.graphicsmill.com/samples/PSDFormat/compiled/Debug/ReplaceBackground_pdf>) (download)
- [ReplaceBackground.png](<https://www.graphicsmill.com/samples/PSDFormat/compiled/Debug/ReplaceBackground_png>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Codecs.ImageReader](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_ImageReader.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.CloseOnDispose](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_ImageReader_CloseOnDispose.htm.md>)
- [Aurigma.GraphicsMill.Codecs.ImageReader.Create(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_ImageReader_Create_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.Codecs.Psd.FrameType](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_Psd_FrameType.htm.md>)
- [Aurigma.GraphicsMill.Codecs.Psd.PsdFrame.Type](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_Psd_PsdFrame_Type.htm.md>)
- [Aurigma.GraphicsMill.Drawing.CustomFontRegistry.Add(String)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_CustomFontRegistry_Add_1_bb3a7a4f.htm.md>)
- [Aurigma.GraphicsMill.Drawing.FontAddResult.PostscriptName](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_FontAddResult_PostscriptName.htm.md>)
- [Aurigma.GraphicsMill.Drawing.FontRegistry.FallbackFonts](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_FontRegistry_FallbackFonts.htm.md>)
- [Aurigma.GraphicsMill.Templates.FontResolver.FontRegistry](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Templates_FontResolver_FontRegistry.htm.md>)
- [Aurigma.GraphicsMill.Templates.PsdFrameExtensions.ToGraphicsContainer(PsdFrame,Pipeline,ResizeMode)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Templates_PsdFrameExtensions_ToGraphicsContainer_3_2f902f24.htm.md>)
- [Aurigma.GraphicsMill.Templates.PsdProcessor](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Templates_PsdProcessor.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ResizeMode](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_ResizeMode.htm.md>)
