---
title: "Recolor Red Strokes"
canonical: https://www.graphicsmill.com/samples/Codecs/VectorFormats/RecolorRedStrokes_cs
product: "Graphics Mill"
source: "Codecs/VectorFormats/RecolorRedStrokes.cs"
---

# Recolor Red Strokes

Demonstrates how to modify the stroke colors of shapes within a graphics container by detecting red strokes and recoloring them.

## Instruction

Demonstrates how to edit the stroke color of shapes in a graphics container by detecting red strokes and recoloring them.

## Usings

```csharp
using System.Linq;
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Drawing;
```

## Code Snippet

```csharp
public static class Extensions
{
    public static bool IsRed(this Color color)
    {
        var rgbColor = (RgbColor)color.Convert(PixelFormat.Format24bppRgb);

        return rgbColor.R > (rgbColor.G + rgbColor.B);
    }
}

internal class RecolorRedStrokes
{
    public static void Run()
    {
        using (var reader = new PdfReader("GraphicsContainer.pdf"))
        using (var gc = reader.Frames[0].GetContent())
        using (var writer = new PdfWriter("GraphicsContainer_RecolorRedStrokes.pdf"))
        using (var gr = writer.GetGraphics())
        {
            writer.AddPage(gc.Width, gc.Height, gc.DpiX, gc.DpiY);

            foreach (var shapeItem in gc.Items.OfType<ShapeItem>())
            {
                if (shapeItem.Pen != null && shapeItem.Pen.Color.IsRed())
                {
                    shapeItem.Pen = new Pen(RgbColor.DarkMagenta, shapeItem.Pen.Width);
                }
            }

            gr.DrawContainer(gc, 0, 0);
        }
    }
}
```

## Explanation

The following code:

- Defines an extension method `IsRed` to check if a color is predominantly red.
- Loads a PDF file and extracts its graphics container content.
- Iterates through all shape items and modifies the pen color if the stroke is red.
- Replaces red strokes with dark magenta strokes.
- Renders the modified graphics container to a new PDF using `PdfWriter`.

## Tags

- [Modify Container](<https://www.graphicsmill.com/samples/tags/modify-container_md>): Editing items inside graphics containers
- [PDF](<https://www.graphicsmill.com/samples/tags/pdf_md>): Importing and exporting PDF documents
- [Write](<https://www.graphicsmill.com/samples/tags/write_md>): Writing images with codecs

## Input

- [GraphicsContainer.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/GraphicsContainer_pdf>) (download)

## Output

- [GraphicsContainer\_RecolorRedStrokes.pdf](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/GraphicsContainer_RecolorRedStrokes_pdf>) (download)
- [GraphicsContainer\_RecolorRedStrokes.png](<https://www.graphicsmill.com/samples/Codecs/compiled/Debug/GraphicsContainer_RecolorRedStrokes_png>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Codecs.PdfFrame.GetContent](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfFrame_GetContent.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfReader](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfReader.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfReader.Frames](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Codecs_PdfReader_Frames.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter.AddPage(Single,Single,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_AddPage_4_37f6c741.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter.GetGraphics](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_GetGraphics.htm.md>)
- [Aurigma.GraphicsMill.Color](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Color.htm.md>)
- [Aurigma.GraphicsMill.Color.Convert(PixelFormat)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Color_Convert_1_3e209c07.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawContainer(GraphicsContainer,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawContainer_3_098aa7ce.htm.md>)
- [Aurigma.GraphicsMill.Drawing.GraphicsContainer.DpiX](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_GraphicsContainer_DpiX.htm.md>)
- [Aurigma.GraphicsMill.Drawing.GraphicsContainer.DpiY](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_GraphicsContainer_DpiY.htm.md>)
- [Aurigma.GraphicsMill.Drawing.GraphicsContainer.Items](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_GraphicsContainer_Items.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Pen](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_Pen.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Pen.Color](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_Pen_Color.htm.md>)
- [Aurigma.GraphicsMill.Drawing.ShapeItem](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_ShapeItem.htm.md>)
- [Aurigma.GraphicsMill.Drawing.ShapeItem.Pen](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_ShapeItem_Pen.htm.md>)
- [Aurigma.GraphicsMill.RgbColor.B](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_RgbColor_B.htm.md>)
- [Aurigma.GraphicsMill.RgbColor.G](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_RgbColor_G.htm.md>)
- [Aurigma.GraphicsMill.RgbColor.R](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_RgbColor_R.htm.md>)
