[]
        
(Showing Draft Content)

Blend Modes

Blend modes are used in computer graphics to determine how colors are blended, or mixed, with each other when drawing on a surface that already contains color information. In other words, a blend mode determines the resulting color of a colored pixel when another color is applied to it. The default is BlendMode.Normal, which simply replaces the original color with the new one.

The GcPdfGraphics class supports blend modes by providing PDF-specific overrides of its base GcGraphics class's abstract members:

  • BlendMode Property: Gets or sets the current blend mode. The blend mode that is set using this property remains in effect for all the subsequent drawing on the current GcPdfGraphics, until changed to another value. Note that the current blend mode affects all drawing on the graphics (including text), not just images.

  • IsBlendModeSupported Method: All blend modes are not supported in PDF (in particular, Hue, Saturation, Color and Luminosity are not supported). This method allows to programmatically check whether a particular blend mode is supported or not. An attempt to set an unsupported blend mode will throw an exception.

The below image shows different blend modes applied to two images in a PDF document:


To apply blend modes:

  1. Load images on which blend modes will be applied using FromFile method of Image class.

  2. Create a text layout which will be used to add captions for different blend modes by instantiating CreateTextLayout method and using different properties of TextLayout class.

  3. Apply different blend modes on the loaded images by using the BlendMode property and rendering all the images in a grid.

    var doc = new GcPdfDocument();
    var page = doc.NewPage();
    var g = page.Graphics;
    
    var iorchid = Image.FromFile(Path.Combine("Resources", "ImagesBis", "orchid.jpg"));
    var ispectr = Image.FromFile(Path.Combine("Resources", "ImagesBis", "spectrum-pastel-500x500.png"));
    
    const int margin = 36;
    const int NCOLS = 4;
    var w = (int)((page.Size.Width - margin * 2) / NCOLS);
    var h = (int)((iorchid.Height * w) / iorchid.Width);
    
    // Text layout for captions:
    var tl = g.CreateTextLayout();
    tl.DefaultFormat.Font = Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
    tl.DefaultFormat.FontSize = 12;
    tl.ParagraphAlignment = ParagraphAlignment.Center;
    tl.TextAlignment = TextAlignment.Center;
    tl.MaxWidth = w;
    tl.MaxHeight = h;
    tl.MarginTop = h - g.MeasureString("QWERTY", tl.DefaultFormat).Height * 1.4f;
    
    int row = 0, col;
    // Render all blending modes in a grid:
    var modes = Enum.GetValues(typeof(BlendMode));
    for (int i = 0; i < 2; ++i)
    {
        row = col = 0;
        Image iback, ifore;
        if (i == 0)
        {
            iback = ispectr;
            ifore = iorchid;
        }
        else // i == 1
        {
            iback = iorchid;
            ifore = ispectr;
            page = doc.Pages.Add();
            g = page.Graphics;
        }
        foreach (var mode in modes)
        {
            var blendMode = (BlendMode)mode;
            if (!g.IsBlendModeSupported(blendMode))
                continue;
    
            int x = margin + w * col;
            int y = margin + h * row;
            var r = new RectangleF(x, y, w, h);
    
            g.BlendMode = BlendMode.Normal;
            g.DrawImage(iback, r, null, ImageAlign.StretchImage);
            g.BlendMode = blendMode;
            g.DrawImage(ifore, r, null, ImageAlign.StretchImage);
            g.BlendMode = BlendMode.Normal;
    
            // Caption:
            tl.Clear();
            tl.Append(blendMode.ToString());
            tl.PerformLayout(true);
            var rc = tl.ContentRectangle;
            rc.Offset(x, y);
            rc.Inflate(4, 2);
            g.FillRectangle(rc, Color.White);
            g.DrawTextLayout(tl, new PointF(x, y));
            nextRowCol();
        }
    }
    doc.Save(stream);
    //
    void nextRowCol()
    {
        if (++col == NCOLS)
        {
            col = 0;
            ++row;
        }
    }

Limitation

The Hue, Saturation, Color, and Luminosity blend modes are not supported in GcPdf as they are not supported in the PDF specification.