[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

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

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

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.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.