Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Working with Icons in GDI+
WhatsApp
Mahesh Chand
15y
14k
0
0
100
Article
Working with Icons.zip
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: 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.
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.
Icons in GDI+
classs Icon property
C#
Up Next
Ebook Download
View all
Blockchain Questions and Answers
Read by 263 people
Download Now!
Learn
View all
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.
Membership not found