ARTICLE

Drawing Icons in GDI+

Posted by Dinesh Beniwal Articles | GDI+ in VB.NET December 05, 2009
In this article I will explain how to draw a Drawing Icons in GDI+ using C#.
 
Reader Level:

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


Imports System
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 icon1 As New Icon("C://setup.ico")
        Dim icon2 As New Icon("c://chat.ico")
        Dim x As Integer = 20
        Dim y As Integer = 50
        e.Graphics.DrawIcon(icon1, x, y)
        Dim rect As New Rectangle(100, 200, 400, 400)
        e.Graphics.DrawIconUnstretched(icon2, rect)
    End Sub
End Class

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.

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.
    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!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor