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

Join the conversation! Your thoughts help the community grow.