Home > Articles > Programming > Windows Programming

Working with Bitmapped Graphics in the .NET Framework

  • Print
  • + Share This
Many developers view graphics programming as something best left to other developers, but the .NET Framework contains a vast array of tools to make graphics easier.
Like this article? We recommend

Many developers view graphics programming with the same passion as a visit to the dentist for a root canal. In fact, I probably wouldn't be too far off saying that at least some developers would choose the root canal sans Novocain. It's true that, when working in the Win32 environment, most graphics programming is difficult—to say the least. However, the .NET Framework provides access to these features in a manner that makes them much easier to use. This article won't show every .NET Framework feature, but it demonstrates enough techniques so you can get going on your latest graphics project without the usual fear and consternation.

Loading Bitmaps

Bitmaps are very easy to load in .NET. Any control that includes an Image property can act as a repository for a bitmap. For example, you can put a bitmap onto the face of a pushbutton, if you want. Even odd controls that you might not expect to support a bitmap (such as a check box) include support for them. To add a bitmap at design time, click the ellipsis (...) in the Image property to display the Open dialog box; then select the image you want to use. The .NET Framework supports bitmaps with the BMP, GIF, JPG/JPEG, PNG, ICO, EMF, and WMF extensions, so you have a wide range of file types from which to choose.

Programmatically loading a bitmap is just about as easy as setting the property during design time—if you don't need to add any special functionality. Most developers rely on the PictureBox control to display images, because this particular control provides a little extra functionality that other controls don't have; for example, you have better access to drawing methods. You can load a bitmap in a number of ways. Here are some examples:

pbBitmap.Image = new Bitmap("ColorBlk.gif");
pbBitmap.Image = Image.FromFile("ColorBlk.gif");

Either technique works fine. However, the Image.FromFile() method is probably a little more efficient because you don't create a new object. On the other hand, loading the image this way presents it as the designer created it, which isn't necessarily what you want. Sometimes you want to change the way the image displays when you load it so that it has a particular appearance. Here are four methods you can use to manipulate the image or its environment:

private void btnSize_Click(object sender, System.EventArgs e)
  // Load the image.
  pbBitmap.Image = Image.FromFile("ColorBlk.gif");

  // Show the image centered.
  pbBitmap.SizeMode = PictureBoxSizeMode.CenterImage;
  MessageBox.Show("Center Image");

  // Show the image in its normal position and size.
  pbBitmap.SizeMode = PictureBoxSizeMode.Normal;

  // Stretch the image to fit the area.
  pbBitmap.SizeMode = PictureBoxSizeMode.StretchImage;
  MessageBox.Show("Stretch Image");

  // Change the size of the PictureBox control to fit the image.
  pbBitmap.SizeMode = PictureBoxSizeMode.AutoSize;

You have a number of decisions to make. The first is how to display the image:

  • Most developers choose to display the image in the upper-left corner of the display area, but nothing says that you can't center it (CenterImage). In fact, some graphics applications center the image when it's smaller than the display area and the application displays a number of images in the same-sized boxes.

  • The Normal option displays the image in the upper-left corner of the display area. The image and the display area remain the same size. This is the default setting.

  • Sometimes you want the image to fill the entire display area. Use the StretchImage option to force the image to fit within the confines of the display area. Unfortunately, this option introduces some amount of distortion in most cases, so it's the option of choice when image clarity isn't a factor, such as when you display wallpaper.

  • The AutoSize option changes the size of the display area to match the image. This might sound like an optimal solution, but large images can cover controls on your application, making them inaccessible. The AutoSize option is only useful when you can guarantee that the user won't attempt to load oversized images.

Another way to load a bitmap is as a thumbnail. Many applications rely on thumbnail images as a way to let a user select a particular image from a group of images. The thumbnail images are generally the same size, even when the size of the actual images vary. Here's a technique for loading a bitmap as a thumbnail:

private void btnThumbnail_Click(object sender, System.EventArgs e)
  // Load the image.
  Image Temp = Image.FromFile("ColorBlk.gif");

  // Display it as a thumbnail.
  pbBitmap.Image = Temp.GetThumbnailImage(128, 128, null, IntPtr.Zero);

Notice that you must create an actual Image object (Temp, in this case) and use it to generate the thumbnail that eventually appears onscreen. The GetThumbnailImage() method accepts the size of the thumbnail as a minimum. The third argument is a callback routine of type GetThumbnailImageAbort, which isn't implemented in the Graphics Driver Interface Plus (GDI+) version 1.0. The fourth argument is data for the callback routine. Set these values, as shown, when you use GDI+ 1.0 or don't need the services of the callback routine (generally you don't).

  • + Share This
  • 🔖 Save To Your Account