The e.Graphics object's DrawImage method draws a picture. To print an image, simply use DrawImage to print it on the PrintDocument's surface.
// Print an image. private void pdocImage_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { // Print in the upper left corner at its full size. e.Graphics.DrawImage(picImage.Image, e.MarginBounds.X, e.MarginBounds.Y, picImage.Image.Width, picImage.Image.Height);
// Print in the upper right corner, // sized to fit beside the other image. int left = e.MarginBounds.Left + picImage.Image.Width; int width = e.MarginBounds.Width - picImage.Image.Width; float scale = width / (float)picImage.Image.Width; int height = (int)(picImage.Image.Height * scale); e.Graphics.DrawImage(picImage.Image, left, e.MarginBounds.Y, width, height);
// Print the same size in the lower right corner. int top = e.MarginBounds.Bottom - height; e.Graphics.DrawImage(picImage.Image, left, top, width, height); }
This code uses DrawImage three times to print the image in three places in different sizes.
This code specifies the X and Y coordinate for the upper left corner where you want the image drawn and the image's width and height. You can get more explicit control if you use Rectangles to specify the part of the image to draw and the location where you want it drawn. If the two Rectangles have different aspect ratios, DrawImage stretches the image to fit the destination area.
Comments