WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

System.Drawing with C#
Pages: 1, 2, 3

Graphics Class Properties

The following are some of the most important properties of the Graphics class:



DpiX

The horizontal resolution of the current Graphics object.

DpiY

The vertical resolution of the current Graphics object.

PageUnit

By default, the measurement unit of a Graphics object is pixels. You can change this by setting the PageUnit property. This property takes one of the members of the GraphicsUnit enumeration: Display, Document, Inch, Millimeter, Pixel, Point, or World. For example, the following changes the PageUnit of a Graphics object into inches:


graphics.PageUnit = GraphicsUnit.Inch;

The Font Class

The Font class represents a font. The easiest way to construct a Font object is by passing a string containing the font name and the font size. For instance, the following obtains the Graphics object of a form, creates an Arial font with the size of 14, creates a SolidBrush called "solidBrush," and writes a string on the Graphics object in 14 point Arial:

Graphics g = this.CreateGraphics();
Font font = new Font("Arial", 14);
SolidBrush solidBrush = new SolidBrush(Color.Chartreuse);
g.DrawString("Hello", font, solidBrush, 10, 100);

If the font name passed to the constructor does not correspond to an available font, the default font is used. The Font class has read-only Bold and Italic properties that indicate whether or not the font is bold or italic.

The Image Class

Image is an abstract class that encapsulates functionality to work with a raster image. The Image class is used in drawing, as well as to display an image in a form-based program. The System.Windows.Forms.Control class has a BackgroundImage property that can take an Image object as the background image for that control.

Even though Image is an abstract class, it has static methods that enable you to create an Image object. Those methods are FromFile and FromStream. The FromFile method is used to create an Image object directly from an image file, such as a .bmp or .gif file. For example, the following code creates an Image object using the FromFile method by passing the complete path to a .bmp file and assigns the Image object to the BackgroundImage property of a form:


Image image = Image.FromFile("C:\\TV.bmp");
this.BackgroundImage = image;

Alternatively, you can use the FromStream method to create an Image object by passing a System.IO.Stream object.

Another way to construct an Image object is by instantiating one of its derived classes, Bitmap or Metafile. Bitmap is used more often, and you can find more information on the Bitmap class in the following section.

The Image class has three read-only properties related to its size: Width, Height, and Size. Both Width and Height return an integer. The Size property returns a Size structure, the members of which include Width and Height. Once you have an Image object, you can save it back into a file, as shown in the following code:


Image bitmap = Image.FromFile("C:\\TV.bmp");
bitmap.Save("C:\\TV2.bmp");

Another useful method is RotateFlip, which you can use to rotate and flip an Image object. With this method, you can rotate, flip, or rotate and flip. The method accepts one of the following members of the RotateFlipType enumeration:


Rotate180FlipNone, Rotate180FlipX, Rotate180FlipXY, 
Rotate180FlipY, Rotate270FlipNone, Rotate270FlipX, 
Rotate270FlipXY, Rotate270FlipY, Rotate90FlipNone, 
Rotate90FlipX, Rotate90FlipXY, Rotate90FlipY, 
RotateNoneFlipNone, RotateNoneFlipX, RotateNoneFlipXY, and 
RotateNoneFlipY.

The Bitmap Class

The Bitmap class represents a raster or bitmap-based image. The easiest way to instantiate a Bitmap object is to pass the complete path to a bitmap file. For example, the following code instantiates a Bitmap object from a .bmp file named TV.bmp in the C:\ directory, and assigns the object as the background image of a form:


Image bitmap = new Bitmap("C:\\TV.bmp");
this.BackgroundImage = bitmap;

Because Bitmap is derived from Image, it inherits the Image class's properties and methods. However, the Bitmap class offers much more functionality. For example, there are 12 constructors that give you flexibility in creating a Bitmap object. As you have seen, you can pass in the complete path to a bitmap file. However, you can also pass in a System.IO.Stream object or an Image object, and you can change the size of the image. For instance, the following code takes an Image object and sets the size of the resulting Bitmap to 30, 30, regardless of the image's original size :


Size size = new Size(30, 30);
Image image = Image.FromFile("C:\\TV.bmp");
Bitmap bitmap = new Bitmap(image, size);

You can even create a Bitmap object from a Graphics object by using the following constructor:


public Bitmap (
  int width, int height, Graphics g
);

The power of the Bitmap class also derives from two methods that allow you to manipulate a bitmap pixel by pixel: GetPixel and SetPixel. The GetPixel method returns the Color of a specified pixel at a particular coordinate, and SetPixel allows you to change the color of an individual pixel. For example, the following code removes the green component of a Bitmap object, then assigns the bitmap to a form's BackgroundImage property:


Bitmap bitmap = new Bitmap("C:\\TV2.bmp");
int width = bitmap.Width;
int height = bitmap.Height;

int i, j;
for (i = 0; i< width; i++) 
{
  for (j=0; j<height; j++)
  {
     Color pixelColor = bitmap.GetPixel(i, j);
     int r = pixelColor.R; // the Red component
     int b = pixelColor.B; // the Blue component
     Color newColor = Color.FromArgb(r, 0, b);
     bitmap.SetPixel(i, j, newColor);
    }
}
this.BackgroundImage = bitmap;

Summary

This article presents the most important members of the System.Drawing namespace. Its classes and structures are useful for drawing the user interface of a control as well as for printing. Some of the structures, such as Point, Size, and Color, are always used in Windows forms.