Convert fixedPage into image source in my wpf application

34 Views Asked by At

I need to convert my fixed page into a bitmapImage object. This is my code ( I found it [here][1]):

private ImageSource ConvertFixedPageToImageSource()
{
    DocumentPaginator paginator = _Document.DocumentPaginator;
    DocumentPage docPage = paginator.GetPage(CurrentPage);
    RenderTargetBitmap renderTarget =
        new RenderTargetBitmap((int)docPage.Size.Width,
                                (int)docPage.Size.Height,
                                96,
                                96,
                                PixelFormats.Pbgra32);

    renderTarget.Render(docPage.Visual);

    PngBitmapEncoder encoder = new PngBitmapEncoder();

    encoder.Frames.Add(BitmapFrame.Create(renderTarget));
    MemoryStream memoryStream = new MemoryStream();
    encoder.Save(memoryStream);
    BitmapImage bitmapImage = new BitmapImage();
    bitmapImage.BeginInit();
    bitmapImage.StreamSource = memoryStream;
    bitmapImage.EndInit();

    return bitmapImage;
}

_Document is a FixedDocument object.

This code only renders the content of fixedPage. I need to render / convert the full page into the BitmapImage so it could be used as a source for Image control in wpf application. It should include the margins, position of the content in the bitmapImage and not just the visuals. [1]: Render FixedPage contents onto Bitmap

0

There are 0 best solutions below