Creating a Colored Brush in GDI+. | | It is not intuitively obvious how to do many things in .NET. One of these is the methodology for creating a colored brush after retreiving a color from the Color Dialog.
If you are writing code to print in .NET, you know that you have to develop the code to DrawString in order to print text. The following code is used in VB.NET to print some text.
Dim b As New SolidBrush(Color.FromName(.Color))
e.Graphics.DrawString(printObject.Text, _
printFont, _
Color.Black, _
XPos, _
YPos, _
New StringFormat())
|
The Color.Black is a System.Drawing.Brush that will print in Black. However, if you want to print in another color, you would use a Color Dialog to prompt the user for a color. The color dialog will return the color red in a property called Color. If you convert that color property to string, it will convert to a string as "Color [Red]". The problem then is how to use that color at run-time. You can do that by first parsing the real color "Red" from the strng property. Next, you would pass that converted color string "Red" to the code. The code shown below assumes that you have the "Red" in a String variable, so I will create a string variable to show you how to use that string in the code.
Dim colorString As String = "Red"
|
Next, create a SolidBrush object using the Color.FromName method, which converts the string color to a usable SolidBrush object. This can now be used to print in the color red.
Dim b As New SolidBrush(Color.FromName(ColorString))
e.Graphics.DrawString(printObject.Text, _
printFont, _
b, _
XPos, _
YPos, _
New StringFormat())
| |