This documentation is archived and is not being maintained.

How to: Display Images with the .NET Framework

The following code example modifies the OnPaint event handler to retrieve a pointer to the Graphics object for the main form. The OnPaint function is intended for a Windows Forms application, most likely created with a Visual Studio application wizard.

The image is represented by the Image class. The image data is loaded from a .jpg file using the ImageFromFile method. Before the image is drawn to the form, the form is resized to accommodate the image. The drawing of the image is performed with the GraphicsDrawImage method.

The Graphics and Image classes are both in the System.Drawing namespace.

Note Note

GDI+ is included with Windows XP and is available as a redistributable for Windows NT 4.0 SP 6, Windows 2000, Windows 98, and Windows Me. To download the latest redistributable, see For more information, see the GDI+ SDK documentation at GDI+.

#using <system.drawing.dll>

using namespace System;
using namespace System::Drawing;

virtual Void Form1::OnPaint(PaintEventArgs^ pe) override
    Graphics^ g = pe->Graphics;
    Image^ image = Image::FromFile("SampleImage.jpg");
    Form::ClientSize = image->Size;
    g->DrawImage( image, 0, 0, image->Size.Width, image->Size.Height );