Document Solutions for Imaging
Features / Work with Graphics / Add Glow and Soft Edges
In This Topic
    Add Glow and Soft Edges
    In This Topic

    DsImaging provides ApplyGlow method in GrayscaleBitmap class that will be used to add glow effect as well as soft edges to graphics. The glow effect inflates all non-transparent areas of the image by the specified amount and then applies the Gaussian blur to make the border smooth. The soft edges effect deflates non-transparent areas, then applies the Gaussian blur. The glow and soft edges effects are usually applied to a full-color image within a GcBitmap. The first parameter of the method is set to a positive value (for glow effect) or a negative value (for soft edges effect) as per the requirement.

    Glow Effect Soft Edges Effect

    Refer to the following example to apply the glow effect:

    C#
    Copy Code
    // Initialize TextLayout.    
    var tl = new TextLayout(96f);
    
    // Configure text format.
        var f1 = new TextFormat
        {
            FontName = "Calibri",
            FontSize = 120,
            ForeColor = Color.DarkOrchid,
            FontBold = true
        };
        var f2 = new TextFormat(f1)
        {
            ForeColor = Color.White,
            StrokePen = new Pen(Color.DarkOrchid, 3)
        };
    
    // Append the text.
        tl.Append("Grape", f1);
    tl.Append("City", f2);
    
    // Initialize GcBitmap.
    using var bmp = new GcBitmap(880, 390, false);
    
    // Create the graphic using text layout defined.
    using (var g = bmp.CreateGraphics(Color.DarkGray))
    {
        g.DrawTextLayout(tl, new PointF(100, 80));
    }
    
    // Save the image without glow.
    bmp.SaveAsPng("WithoutGlow.png");
    
    // Draw the text on a transparent bitmap at first.
    using (var g = bmp.CreateGraphics(Color.Transparent))
    {
        g.Renderer.SlowAntialiasing = true;
        g.DrawTextLayout(tl, new PointF(100, 80));
    }
    
    // Convert the image to a transparency mask.
    using var gs = bmp.ToGrayscaleBitmap(ColorChannel.Alpha);
    
    // Apply the glow effect to the transparency mask.
    gs.ApplyGlow(4, 6);
    
    /* Map a shadow from the transparency mask to the source GcBitmap drawing opaque pixels with glow color (yellow).
    Apply some additional transparency.*/
    gs.ToShadowBitmap(bmp, Color.Yellow, 0.8f);
    
    // Fill the background.
    bmp.ConvertToOpaque(Color.Gray);
    
    // Draw the text over the prepared background.
    using (var g = bmp.CreateGraphics())
    {
        g.Renderer.SlowAntialiasing = true;
        g.DrawTextLayout(tl, new PointF(100, 80));
    }
    
    // Save the image with glow.
    bmp.SaveAsPng("WithGlow.png");
    

    Refer to the following example to apply the soft edges effect:

    C#
    Copy Code
    // Initialize PathBuilder.
    var pb = new PathBuilder();
    pb.BeginFigure(100, 350);
    pb.AddLine(210, 310);
    
    // Define an arc.
    var arc = new ArcSegment
    {
        Size = new SizeF(183, 173),
        SweepDirection = SweepDirection.Clockwise,
        Point = new PointF(550, 205),
    };
    
    // Add arcs and lines.
    pb.AddArc(arc);
    pb.AddLine(650, 170);
    pb.AddLine(680, 250);
    pb.AddLine(575, 285);
    arc.Point = new PointF(240, 390);
    pb.AddArc(arc);
    pb.AddLine(130, 430);
    pb.EndFigure(true);
    pb.Figures.Add(new EllipticFigure(new RectangleF(295, 197, 200, 190)));
    var gpFill = pb.ToPath();
    var gpStroke = gpFill.Widen(new Pen(Color.Black, 20));
    
    // Draw the image.
    using var bmp = new GcBitmap(800, 600, false);
    var renderer = bmp.EnsureRendererCreated();
    
    bmp.Clear(Color.RosyBrown);
    renderer.FillPath(gpFill, Color.CornflowerBlue);
    renderer.FillPath(gpStroke, Color.Moccasin);
    
    // Save the image without soft edges.
    bmp.SaveAsPng("WithoutSoftEdges.png");
    
    // Draw the figure on a transparent background.
    bmp.Clear(Color.Transparent);
    renderer.FillPath(gpFill, Color.CornflowerBlue);
    renderer.FillPath(gpStroke, Color.Moccasin);
    
    // Convert the image to the transparency mask.
    using var gs = bmp.ToGrayscaleBitmap(ColorChannel.Alpha);
    
    // Apply the soft edges effect to the transparency mask.
    gs.ApplyGlow(-4, 8);
    
    // Draw the original image.
    bmp.Clear(Color.RosyBrown);
    renderer.TransparencyMaskBitmap = gs;
    renderer.FillPath(gpFill, Color.CornflowerBlue);
    renderer.FillPath(gpStroke, Color.Moccasin);
    
    // Save the image with soft edges.
    bmp.SaveAsPng("WithSoftEdges.png");