Printing a WPF BitmapImage
Asked Answered
I

3

6

What's the best way to print a BitmapImage? I come from a background in System.Drawing so I was thinking about converting it to a Bitmap and then printing it, but I'm thinking there's probably a better way.

Thanks!

Illomened answered 2/11, 2009 at 15:35 Comment(0)
G
5

Building upon Drew's answer, it is best to measure and arrange the container that is handed to the PrintVisual method. This will prevent an image that is larger than a 8.5 x 11 sheet of paper from being cut off. Here is an example of how I printed an image that was partially visible on-screen:

PrintDialog dlg = new PrintDialog();
bool? result = dlg.ShowDialog();

if (result.HasValue && result.Value)
{
    ImageViewer.Measure(new Size(dlg.PrintableAreaWidth, dlg.PrintableAreaHeight));
    ImageViewer.Arrange(new Rect(new Point(0, 0), ImageViewer1.DesiredSize));

    dlg.PrintVisual(ImageViewer, "Print a Large Image");
}

ImageViewer in my example could be replaced with any UIElement container such as a stackpanel, canvas, grid, ect. ImageViewer.Source should be set to the BitmapImage that is ready to be printed.

I got the idea from this page: http://www.switchonthecode.com/tutorials/printing-in-wpf

Gaze answered 3/11, 2009 at 16:23 Comment(1)
Thank you very much. This resolved my problem with printing an image.Counter
F
1

Check out the PrintDialog class. All you should need to do is call the PrintVisual method passing in an Image as the visual that has your BitmapImage as a source.

You may want to setup other printing options, but you'll discover those as you explore PrintDialog and related APIs.

Frechette answered 2/11, 2009 at 16:24 Comment(0)
A
0

illogial's answer has the problem, that's always stretching the image to pagesize and cuts off some content at trying this.


Therefore i wrote a own solution, which only stretch(is this the right word?) if the image is to large, can use multiply copies and pageorientations.

Here is the Code

Alva answered 12/8, 2020 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.