Ziff-Davis Enterprise 
DevSource: Microsoft Developer Resource
Add OnsArchitectureLanguagesTechniquesUsing VSForums
 
Home arrow Languages arrow GDI+ Image Handling in C#
GDI+ Image Handling in C#
By Rick Leinecker

Rate This Article:
Add This Article To:
GDI+ Image Handling in C#
( Page 1 of 3 )

Learn how to easily handle images of different formats using GDI+ in C#.

It was a dream come true when I learned in July 2000 that the .NET framework included image loading, saving, and manipulation. Before that time, you had to either buy a third-party library or write your own code to load and save images. I did both at various times, and each had their fair share of pitfalls.

This article shows you how to use images in your application by calling on the GDI+ classes that provide the needed functionality. It's so easy now that it's hard for me to convince new programmers that in the past, images were difficult to manage.

ADVERTISEMENT

The Graphics Class

As with all GDI+ methods, we'll draw to a surface (similar to a canvas). The surface can be anything including the physical screen, a printer, or an in-memory bitmap. The Graphics class abstractly represents surfaces so that programmers can forget about the exact surface device. All drawing is done in the same way.

Click here for a more complete discussion on the Graphics class.

We'll get started learning how to use the Graphics class by creating a simple Windows C# application named LearnImagesClass. Once the application project has been created, we'll go to the form properties, click on the events button (the lightning bolt), and add an OnPaint event handler.

The default method that is created contains a sender object (which I have never used in this context) and a PaintEventArgs object. The wizard-created method is shown below:

private void OnPaint(object sender, PaintEventArgs e)
{

}

One of the members of the PaintEventArgs object is a Graphics object named Graphics. All methods and properties of this Graphics object can be accessed as e.Graphics.SomeGDIMethodCall(). The following example uses the Graphics object to draw a line:

e.Graphics.DrawLine(new Pen(Color.Black), 0, 0, 250, 250);



 
 
>>> More Languages Articles          >>> More By Rick Leinecker
 



DevSource video
Devsource Video Series
Manipulating Society through Technology
Jeremy Bailenson, Director of the Virtual Human Interaction Lab at Stanford University, talks about virtual reality, avatars, Moore's law, how real world behaviors influence online reality, and societal manipulation through technology!
>> Play video
>> Read article
>> See all videos
DevLife Blog

Julia explores the Robotics Studio! (It's for more than you think.)

MSDev Blog

Messages for Bill Gates!

Make it Work
.NET makes runtime type checking a breeze. See what Peter has to say about it in this week's tips!
News
Microsoft Counts on App Support for Vista
Microsoft has taken pains to demonstrate that Windows Vista will have ample application support.
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.