Draw text with colors reversed along a diagonal line in C#

[example]

The post Draw text with colors reversed in its upper and lower halves in C# showed how you can draw text that has upper and lower halves with colors switched. This example uses a similar technique to switch the colors along a diagonal line.

The approach is similar to the one used by the previous example. The program creates two bitmaps that hold the text drawn with the different color combinations. The following code snippet draws the upper right and lower left parts of the drawing area.

// Fill the entire rectangle with the top version.
using (TextureBrush brush = new TextureBrush(bm_top))
{
    gr.FillRectangle(brush, rect);
}

// Fill the lower left corner with the bottom version.
Point[] points = 
{
    new Point(rect.X, rect.Y),
    new Point(rect.X, rect.Bottom),
    new Point(rect.Right, rect.Bottom),
};
using (TextureBrush brush = new TextureBrush(bm_bottom))
{
    gr.FillPolygon(brush, points);
}

Instead of filling two triangular pieces, this code first fills the entire drawing area with the colors that should be in the upper right corner. Filling the upper right corner specifically wouldn’t be too hard, but this is slightly easier.

Next, the code creates an array of points to define the lower left triangular area and uses the Graphics object’s FillPolygon method to fill that area with the lower-left colors.

That’s all there is to it. In my next post, I’ll show how to make one more variation that divides the text into pieces on either side of a sine curve.

See the previous post and download this example to see additional details.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in drawing, fonts, graphics and tagged , , , , , , , , , . Bookmark the permalink.

1 Response to Draw text with colors reversed along a diagonal line in C#

  1. Pingback: Draw text with colors reversed along a sine wave in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.