Blue Theme Orange Theme Green Theme Red Theme
 
Mindcracker MVP Summit 2012
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » GDI+ and Graphics » The ColorConverter and ColorTranslater Classes in GDI+

The ColorConverter and ColorTranslater Classes in GDI+

In this article I will explain about ColorConverter and ColorTranslater Classes in GDI+.

Author Rank :
Page Views : 3056
Downloads : 37
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
WindowsApplication3.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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 Table5.2.

TABLE 5.2 Common TypeConverter methods

Method

Description

CanConvertForm

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

CanConverTo

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

ConvertFrom

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

ConvetTo

Converts a specified object to the new   and   returns the object

GetStanderdValues

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

GetStandardValuesSupported

Identifies whether this object supports a standard set of values.


Listing 5.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 5.4 Using the ColorConverter class to convert colors

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
 
    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = Me.CreateGraphics()
        g.Clear(Me.BackColor)
        Dim str As [String] = "#FF00FF"
        Dim clrConverter As New ColorConverter()
        Dim clr1 As Color = DirectCast(clrConverter.ConvertFromString(str), Color)

        'Use colors
        Dim clr2 As New SolidBrush(clr1)
        Dim clr3 As 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()
    End Sub
End Class

fig5.4.gif
 
FIGURE 5.4: Converting colors

Figure 5.4 shows the output from Listing 5.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 5.3 describes the methods of the ColorTranslator class. All of the methods are static.

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

LISTING 5.5 Translating colors

    Private Sub ColorTranslator_click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim g As Graphics = Me.CreateGraphics()

        'Translate colors
        Dim win32color__1 As Color = ColorTranslator.Formwin32(oxFF0033)
        Dim htmlColor As Color = ColorTranslator.FromHtml("#00AAFF")

        'use Colors
        Dim clr1 As New SolidBrush(win32Color)
        Dim clr2 As New SolidBrush(htmlColor)

        'Draw GDI+
        g.FillEllipse(clr1, 10, 10, 50, 50)
        g.FillRectangle(clr2, 60, 10, 50, 50)

        'dispose of object
        clr1.Dispose()
        clr2.Dispose()
        g.Dispose()
    End Sub

TABLE 5:3 ColorTranslator methods

Method Description

FromHtml

Translates from an HTML color representation to a Color structure

FromOle

Translates from an OLE color value to a Color structure

FromWin32

Translates from an windows color to a Color structure

ToHtml

Translates from a Color structure to HTML color representation.

ToOle

Translates from a Color structure to OLE color.

ToWin32

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.
 
Note: You can also transform colors using transformation methods. Some of transformation methods are for scaling, translating, rotating, and shearing. 

Conclusion

Hope the article would have helped you in understanding ColorConverter and ColorTranslater Classes in GDI+. Read other articles on GDI+ on the website.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dinesh Beniwal
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.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Mindcracker MVP Summit 2012
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.