'How to fill text with 2 different color/texture

I draw text on top of a image using Path.Addstring and filling it with a color and it works perfectly. Now I would like to split(bisect) the text vertically and have 2 different colors or textures. For eg. the top half of the text with a solid brush and the bottom half with hatch brush. I'd like to know if this is possible and which way should I implement it.

Reference image created using paint.net software. I drew a line to split the text and filled the bottom part with a different texture.

Text

*I don't want the line to be visible in the final output.



Solution 1:[1]

Possible.

  1. Fill the path with the solid brush.
  2. Get the rectangle that bounds the path through the GraphicsPath.GetBounds method.
  3. Call the Graphics.SetClip method to exclude the top half of the rectangle.
  4. Fill the path with a TextureBrush or HatchBrush.

An example that uses a HatchBrush to fill the second vertical half of the path.

private void SomeControl_Paint(object sender, PaintEventArgs e)
{
    var g = e.Graphics;
    var r = (sender as Control).ClientRectangle;
            
    using (var gp = new GraphicsPath())
    using (var sf = new StringFormat())
    using (var fnt = new Font("Blackoak Std", 72))
    using (var hbr = new HatchBrush(HatchStyle.Percent25, Color.White, Color.Red))
    {
        sf.Alignment = sf.LineAlignment = StringAlignment.Center;

        gp.AddString("RED", fnt.FontFamily, (int)fnt.Style, GetEmFontSize(fnt), r, sf);

        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.FillPath(Brushes.Red, gp);

        var rf = gp.GetBounds();
        rf.Height /= 2f;

        g.SetClip(rf, CombineMode.Exclude);
        g.FillPath(hbr, gp);
        g.ResetClip();
        g.SmoothingMode = SmoothingMode.None;
    }
}

private float GetEmFontSize(Font fnt) =>
    fnt.SizeInPoints * (fnt.FontFamily.GetCellAscent(fnt.Style) + 
    fnt.FontFamily.GetCellDescent(fnt.Style)) / fnt.FontFamily.GetEmHeight(fnt.Style);

See also the other HatchStyle values.

SOQ72225088A

SOQ72225088B

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dr.null