Home > Guides > Programming > .NET and Windows Programming

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Displaying Images

Last updated Sep 9, 2004.

You display images by calling the DrawImage method of the Graphics object that defines the surface on which you want the image to appear. For example, if you have a form with a panel, panel1, the simplest way to display the image is like this:

[C#]

using (Graphics g = panel1.CreateGraphics())
{
    g.DrawImage(bmp, 0, 0);
}

[Visual Basic]

Dim g as Graphics = Panel1.CreateGraphics()
Try
    g.DrawImage(bmp, 0, 0)
Finally
    g.Dispose()
End Try

That will draw the bitmap at position (0, 0) (the upper left-hand corner) of the panel. This is the easiest, but not necessarily the most efficient way to draw an image. When you specify the position but not the size, GDI performs automatic scaling of the image. GDI scales the image using the resolution ("pixels per inch", defined in the image as HorizontalResolution and VerticalResolution) values of the image and the display device. If the image's resolution doesn't match the display device's resolution (which is usually 96 pixels per inch), then GDI will perform automatic scaling.

You can avoid automatic scaling by specifying a rectangle in which GDI is to display the image. If you want to display the image in its full size, you simply write:

[C#]

g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height);

[Visual Basic]

g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height);

There are 30 overloads of Graphics.Draw that let you specify the location and size of the image in different ways. You can scale, crop, rotate, reflect, and skew images to create many different effects. See the Graphics.Draw documentation the .NET Framework SDK Reference for full details.

Creating Bitmaps

In the previous sections you've seen how to work with images that already exist. But suppose you want to create your own bitmap image, perhaps as part of a paint program. How would you do that?

The easy part is creating the bitmap. You can create a blank Bitmap object instance by calling the Bitmap constructor, passing it the width and height of the new image. You can also pass it a reference to a Graphics object so that the bitmap will be created with the same resolution.

After you've created the bitmap, you need to be able to manipulate it: draw lines and shapes, and write text to it. You do that by creating a Graphics object that uses the bitmap as its canvas. The Graphics.FromImage method will create such a graphics object. You can then call Graphics methods to create whatever pretty picture you like, and save the bitmap as shown in the previous section. The code below provides an example.

[C#]

using (Bitmap bmp = new Bitmap (100, 100))
{
    using (Graphics g = Graphics.FromImage(bmp))
    {
        // fill it with white
        g.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
        // and draw a black diagonal line
        g.DrawLine(Pens.Black, 0, 0, bmp.Width-1, bmp.Height-1);
    }
    bmp.Save("myimage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}

[Visual Basic]

Dim bmp As New Bitmap(100, 100)
Try
    Dim g As Graphics = Graphics.FromImage(bmp)
    Try
    ' fill it with white
        g.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height)
    ' and draw a black diagonal line
        g.DrawLine(Pens.Black, bmp.Width - 1, 0, 0, bmp.Height - 1)
    Finally
        g.Dispose()
    End Try
    bmp.Save("myimage.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
Finally
    bmp.Dispose()
End Try

Discussions

Copies of the array?
Posted Dec 23, 2008 03:40 PM by luige21
1 Replies
Hi
Posted Dec 5, 2008 05:10 AM by ajay2000bhushan
2 Replies
You have no clue.
Posted Jun 10, 2008 03:28 PM by theinternetmaster
1 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jim Mischel"Highly unlikely" does not mean "impossible"
By Jim MischelJuly 18, 2009 No Comments

One of my programs crashed the other day in a very unexpected place.  A call to System.Threading.ConcurrentQueue.TryDequeue (from the Parallel Extensions to .NET) resulted in an OverflowException being thrown.  Investigation revealed a pretty serious bug in the System.Random constructor.

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part II
By John TraenkenschuhMay 24, 2009 No Comments

In the last blog in this series, Traenk relates his first experiences with computers and with coding.  But now, some years have passed. . .

It's Here; Put Away Your Pre-Conceptions on What an OS Must Be: Part I
By John TraenkenschuhMay 24, 2009 No Comments

Traenk relates his past experience with Operating Systems that goes back 25 years, ok, more than that but he ain't tellin'

See More Blogs

Informit Network