Blue Theme Orange Theme Green Theme Red Theme
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Search :       Advanced Search »
Home » »

Author Rank:
Author Name:
Total downloads :
Total page views :  
Rating :  /5
This article has been rated :   times
  Add to Technorati Add to Technorati    Digg This Digg This    Add to Add to
    Rate this article Read/Post comments Support Us   Printable Version 

Download Files
Chapter Contents
1. Colors, Fonts, and Text
2. Second Page
3. ColorConverter and ColorTranslater Classes

Become a Sponsor



When you create pens using SystemPens, you cannot modify the width or other properties of the pen .The code will compile but will throw an unhandled exception when executed .If you create a pen using SystemColors, however, you can modify its width like this:


Pen pn = new Pen (SystemColors.HighlightText);

pn.Width = 4;


The ColorConverter and ColorTranslater Classes


The ColorConverter class is used to convert colors from one data type to another. This class is inherited from the TypeConverter class, which defines the functionality for conversion of type and accessing values and properties of types. The TypeConverter class serves as a base class for many conversion classes, and ColorConverter and FontConverter are two of them. We will discuss FontConverter in more detail later in this article. Some of the common methods of TypeConverter class (which are available in the ColorConverter class) are described in Table 2.


Table 2: Common TypeConverter methods





Takes a type as a parameter and returns true if the converter can convert an object to the type of the converter; otherwise returns false


Takes a type as a parameter and returns true if the converter can converts an object to a given type; other wise return false.


Converts an object to the type of converter and   returns the converted object


Converts a specified object to the new   and   returns the object


Returns a collection of standard values (collection type) for the data type for which this type converter is designed.


Identifies whether this object supports a standard set of values.




Listing 4 uses the ColorConverter class methods to convert colors. We store in string and call the ConverterFromString method, which returns the Color object. Later we will use the Color objects to create two brushes that we will use to fill a rectangle and an ellipse.


Listing 4: Using the ColorConverter class to convert colors


private void ColorConvert_Click(object sender, System.EventArgs e)


Graphics g = this.CreateGraphics ();

g.Clear (this.BackColor);

String str ="#FF00FF";

ColorConverter clrConverter  =new ColorConverter ();

Color clr1=(Color) clrConverter.ConvertFromString(str);

//Use colors

SolidBrush clr2 =new SolidBrush (clr1);

SolidBrush clr3= new SolidBrush(clr1);

//Draw GDI+ objects

g. FillEllipse (clr2, 10,10,50,50);

g. FillRectangle (clr3, 60, 10, 50, 50);

//Dispose of objects

clr2. Dispose ();

clr3. Dispose ();

g. Dispose ();




Figure 4: Converting colors


Figure 4 shows the output from Listing 4.


The ColorTranslator class provides methods to translate colors to and from HTML, OLE, and Win32 color values. These methods are useful when you are using legacy color structures that pre-date the .NET Framework. For example, you may have legacy code that gives the HTML color representation of a color. Table 3 describes the methods of the ColorTranslator class. All of the methods are static.


Listing 5 uses the ColorTranslator class to translate colors from Win32 and HTML colors. Later these colors will be used to create brushes.


Listing 5: Translating colors


Private void ColorTranslator_Click(object sender,System.EventArgs e)


Graphics g =this.CreateGraphics();

//Translate colors

Color win32Color = ColorTranslator.FromWin32(0xFF0033);

Color htmlColor = ColorTranslator.FromHtml(" #00AAFF");

//use Colors

SolidBrush clr1 =new SolidBrush(win32Color);

SolidBrush clr2 = new SolidBrush(htmlColor);

//Draw GDI+

g.FillEllipse(clr1,10 ,10 ,50 ,50);

g.FillRectangle(clr2,60 ,10 ,50 ,50);

//dispose of object






Table 3: ColorTranslator methods





Translates from an HTML color representation to a Color structure


Translates from an OLE color value to a Color structure


Translates from an windows color to a Color structure


Translates from a Color structure to HTML color representation.


Translates from a Color structure to OLE color.


Translates from a Color structure to window color.




In a manner similar to the “from ” methods just discussed, you can translate a Color structure   into Win32, HTML and OLE values using the Towin32, ToHtml, and ToOle method, respectively.




You can also transform colors using transformation methods. Some of transformation methods are for scaling, translating, rotating, and shearing.


Working with Fonts


In this section we will concentrate on fonts .The discussion starts with a description of the types of fonts in the Windows operating system, followed by a little background material on fonts. After these basic concepts are covered the discussion turns to how fonts are handled in GDI+ and .NET.

Total Pages : 3 123

        Rate this article Read/Post comments Support Us   Printable Version
 Book Detail
Book Title :
Price: US$
EBook Price: US$
Publisher Home Page: [HyperLinkPublisherHomePage]
Book Url: [HyperBookUrl]
 Post a new question or comment about this article
 [Top] Rate this article:-
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
Latest Comments:
Subject Posted By Posted On
Support Us
Tell your friends about us
Is this article is a Bad Submission report us here
Submit articles, tutorials, and tips
Help by answering questions on our discussion forums
Help us correct errors and broken links
Looking for a job? Post your resume here
Looking for developers? Post your job here

 © 2014  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.