Drawing Icons in GDI+


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

The DrawIcon and DrawIconUnstretched methods are used to draw icons. DrawIcon draws an image represented by a specified object at the specified coordinates- stretching the image to fit, if necessary. DrawIconUnstrectched draws an image represented by an Icon object without scaling the image.

DrawIcon and DrawIconUnstretched take two arguments: an Icon object and upper left corner coordinates of a rectangle. To draw an icon using these methods, an application first creates an icon and either a Rectangle object or coordinates to the upper left corner at which to draw the icon.

An Icon object represents a Window icon. An application creates an Icon object using its constructor, which takes arguments of String, Icon, Stream, and Type. Table 3.3 describes the properties of the Icon class.

Table 3.4 describes some of the methods of the Icon class.

TABLE 3.3: Icon properties

Property

Description

Handle

Represents the window handle of an icon.

Height

Represents the height of the icon.

Size

Represents the size of the icon.

Width

Represents the width of the icon.

Clone

Clones an Icon object, creating a duplicate image.

Save

Saves an Icon object to the output stream.

ToBitmap

Convert an Icon object to a Bitmap object.


Listing 3.20 draws icons. The application first creates two Icon objects, then creates a Rectangle object and calls DrawIcon and DrawIconUnstretched.

LISTING 3.20: Drawing icons


using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;

namespace
WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender,
        System.Windows.Forms.PaintEventArgs e)
        {
            Icon icon1 = new Icon("C://setup.ico");
            Icon icon2 = new Icon("c://chat.ico");
            int x = 20;
            int y = 50;
            e.Graphics.DrawIcon(icon1, x, y);
            Rectangle rect = new Rectangle(100, 200, 400, 400);
            e.Graphics.DrawIconUnstretched(icon2, rect);
        }
    }
}


Figures 3.27 shows the output from Listing 3.20

3.27.gif

FIGURE 3.27: Drawing icons

Conclusion

Hope the article would have helped you in understanding how to draw Drawing 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.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.