SIGN UP MEMBER LOGIN:    
ARTICLE

Working with Icons in GDI+

Posted by Mahesh Chand Articles | GDI+ & Graphics February 18, 2010
In this article I will explain about working with Icons in GDI+.
Reader Level:
Download Files:
 

This article has been excerpted from book "Graphics Programming with GDI+".

The Icon class represents a Windows icon, which is a small transparent bitmap. Just like the Bitmap class, this class is inherited from the Image class.

An application can create an Icon object from a stream, string, icon, icon file, or type by using the Icon class constructors with the size of the icon as an optional parameter. The Icon provides four read-only properties-Handle, Height, Size, and Width-which return a window handle to the icon, height, size, and width of an icon, respectively.

Listing 7.21 creates an Icon object from an icon file and sets the icon of a form using the Form class's Icon property.

LISTING 7.21: Creating an icon and setting a form's Icon property

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            //Create an icon
            Icon curIcon = new Icon("D:/VB.NET Basic Practice Projects/WindowsApplication8/Images/ico-32bit/wi0126-48.ico");

            //Set form's icon
            this.Icon = curIcon;

            //Get icon properties
            float h = curIcon.Height;
            float w = curIcon.Width;
            Size sz = curIcon.Size;
        }

The FromHandle method of the Icon class creates an Icon object from a window handle to an icon (HICON). The Save method saves an Icon object to a stream, and the ToBitmap method converts an Icon object to a Bitmap object. Listing 7.22 creates a Bitmap object from an Icon object using ToBitmap and draws the bitmap using DrawImage.

LISTING 7.22: Creating a bitmap from an icon and displaying it

        private void Form1_Paint(object sender, PaintEventArgs e)
        {

            //Create and icon
            Icon curIcon = new Icon("D:/VB.NET Basic Practice Projects/WindowsApplication8/Images/ico-32bit/wi0126-48.ico");

            //Create a bitmap from an icon
            Bitmap bmp = curIcon.ToBitmap();

            //Draw bitmap
            Graphics g = e.Graphics;
            g.Clear(this.BackColor);
            g.DrawImage(bmp, 10, 10);
            g.Dispose();
        }

Figure 7.35 shows the output from Listing 7.21 and 7.22.

Figure 7.35.jpg

FIGURE 7.35: Viewing icons

Sometimes you will need to convert a Bitmap object into an Icon object.

The following code snipped shows how to do this:

            Icon curIcon;
            curIcon = Icon.FromHandle(bmp.GetHicon());

Conclusion

Hope the article would have helped you in understanding working with Icons in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
Nevron Gauge for SharePoint
Become a Sponsor