Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

How to fill text with 2 different color/texture

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);
Comment

PREVIOUS NEXT
Code Example
Csharp :: SerializedObjectNotCreatableException: Object at index 0 is null 
Csharp :: salary, overtime, deductions, gross pay and netpay in console C# 
Csharp :: c# how to use or operator on integers in while loop 
Csharp :: C# string array in setter 
Csharp :: make all variables nonserizlized unity 
Csharp :: backcolor app winform C3 
Csharp :: csharp test for null 
Csharp :: two lowest positive numbers given an array of minimum 
Csharp :: c# uri to string 
Csharp :: how to detect when a gameobject has exited a trigger c# 
Csharp :: Process.Start(osk.exe) 
Csharp :: world space constant size 
Csharp :: the range data annotation attribute (Double) 
Csharp :: Convert C# Class to xml wth xsd.exe 
Csharp :: shell32.dll c# example 
Csharp :: c# code for simplex method 
Csharp :: c# delegates 
Csharp :: asp net route attribute vs httpget 
Csharp :: how to start commvault services on linux 
Csharp :: reflection static method c# 
Csharp :: mvc validate 
Csharp :: linq contains null 
Csharp :: how to change an object color with fill c# 
Csharp :: go down a line in <summary dotnet 
Csharp :: C# JOSN Array Conversion 
Csharp :: virtual properties and lazy loading in c# 
Csharp :: gridview edit update delete in asp.net textbox size 
Csharp :: c# date to julian YYJJJ date 
Csharp :: linq select distinct 
Csharp :: .net return context.Result without extra new objectResult 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =