Print images in C#

[print images]

This example shows a simple way to print images. When you print, the PrintDocument object’s PrintPage event handler provides a Graphics object named e.Graphics. That object’s DrawImage method can draw a picture on the printed surface.

To print an image, simply use DrawImage to print it. The following code shows how this example prints images.

// Print an image.
private void pdocImage_PrintPage(object sender,
    PrintPageEventArgs e)
    // Print in the upper left corner at its full size.
        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);
        left, e.MarginBounds.Y, width, height);

    // Print the same size in the lower right corner.
    int top = e.MarginBounds.Bottom - height;
        left, top, width, height);

This code uses DrawImage three times to print the image in three places at 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 Rectangle structures to specify the part of the image to draw and the location where you want it drawn. If the source and destination Rectangle structures have different aspect ratios (width-to-height ratios), DrawImage stretches the image to fit the destination area.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in graphics, image processing, printing and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.