Drawing an Image

Drawing an image always takes place on a Graphics object. The Graphics.DrawImage method is heavily overloaded to provide many, seemingly confusing options for carrying out this simple task.

There is also a DrawImageUnscaled method that implies that the source and destination rectangles are of the same size.

Keep in mind that Images are drawn with the following considerations:

  • There is a destination rectangle for the image
  • There is a source rectangle that may enclose all or part of the image being drawn.
  • The destination rectangle may be of the same size, larger or smaller in both X and Y dimensions and may have a different aspect ratio to the original source rectangle.
  • When drawn, the image may be modified using an ImageAttributes object which enables many complex operations.
  • All of the DrawImage options do the same thing but parameters may be explicit or implied.
  • The Graphics object may have settings such as a transformation or interpolation filter that affects the image quality and appearance.

The illustrations below show how the DrawImage method is used. "g" is the graphics object obtained by the drawing program. "img" is the image stored in memory

 

A straight copy of an image.

g.DrawImage(img,<point>)

Point is the X,Y coordinate of the top-left corner of the destination rectangle. This is the same size as the source rectangle which, in turn is the same size as the source image.

 

An enlargement. The destination rectangle is proportionately larger than the source. Note that the magnified image looks chunky and pixellated. This effect can be reduced by selecting an anti-aliased drawing mode in the Graphics object.

g.DrawImage(img,ClientRectangle)

The source rectangle is implied (the whole image) and the destination will be the whole window.

 

Selective copy of a sub-portion of an image. The source rectangle selects a bit of the image, the destination rectangle is the same size.

 

destrect=new Rectangle(100,100,10,10)

g.DrawImage(img,destrect,75,40,10,10,GraphicsUnit.Pixel)

 

Note that the GraphicsUnit will always be Pixel

 

Copy and distortion of a portion of the original.

 

destrect=new Rectangle(10,20,120,40)

srcrect=new Rectangle(75,40,10,10)

g.DrawImage(img,destrect,srcrect,GraphicsUnit.Pixel)

 

The source and destination rectangles are explicitly defined.

 

Rotating an image is accomplished by applying a transformation to the Graphics object used to draw the image. In this case, the image was rotated by 30 degrees anticlockwise.

Matrix mx = new Matrix();

 

mx.Rotate(-30);

g.Transform = mx;

g.DrawImage(img,new Point(100,50));

 

No size changes were required so the full source and destination rectangles were just implied.

 

Another type of image distortion you can do with the source and destination rectangle is the flip. Here an image has been drawn upside down by drawing it as if it were reflected in a mirror. To do this, the destination is set up as normal but the origin of the source points to the opposite edge of the image and the height is reversed by negating it.

 

Rectangle dest = new Rectangle(10,10,100,100);

Rectangle src = new Rectangle(0,img.Height,img.Width,-img.Height);

g.DrawImage(img,dest,src,GraphicsUnit.Pixel);

 

Drawing an image transparently is accomplished using the ImageAttributes class. This object can store a ColorMatrix which can be used to alter the alpha of an image as it is being drawn. Amongst its other properties, ColorMatrix.Matrix33 enables you to control the overall transparency of an image.

The image to the left was drawn with the following code:

g.DrawImage(img1,new Point(0,0));

ImageAttributes ia = new ImageAttributes();

ColorMatrix cm = new ColorMatrix();

cm.Matrix33=0.5f;

ia.SetColorMatrix(cm);

g.DrawImage(img2,new Rectangle(0, 0, img2.Width, img2.Height ), 0, 0, img2.Width, img2.Height, GraphicsUnit.Pixel, ia);

Back to the GDI+ FAQ

Copyright Robert W. Powell 2003, All rights reserved