Drawing Icons in GDI+ using VB.NET

In this article I will explain how to draw a Drawing Icons in GDI+ using C#.
  • 2908

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.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.