What is the correct way to print images using WPF DocumentPaginator

890 Views Asked by At

Here is my code that is in a class extending DocumentPaginator

    public override DocumentPage GetPage(int pageNumber)
    {
        BitmapImage source = new BitmapImage();
        using (Stream stream = new FileStream(GetPagePath(pageNumber), FileMode.Open))
        {
            source.BeginInit();
            source.StreamSource = stream;
            source.CacheOption = BitmapCacheOption.OnLoad;
            source.EndInit();
        }

        var image = new Image { Source = source };

        Rect contentBox = new Rect(PageSize);

        return new DocumentPage(image, PageSize, contentBox, contentBox);
    }

However when I actually run this code, it doesn't seem to load my image and merely prints blank pages.

What is the correct way to load my image and attach it to a DocumentPage object?

1

There are 1 best solutions below

0
Clemens On

You must do the layout of the Image control by calling its Measure() and Arrange() methods:

var image = new Image { Source = source };
var size = new Size(source.PixelWidth, source.PixelHeight);
image.Measure(size);
image.Arrange(new Rect(size));