PrintVisual prints empty Page

Viewed 324

I am trying to print some .png files directly to the printer.

Therefore I set up this function:

    private void Print(string filepath)
    {
        LocalPrintServer printServer = new LocalPrintServer();
        PrintQueue printQueue = printServer.GetPrintQueue("PDFCreator");

        BitmapImage image = new BitmapImage();
        image.BeginInit();
        image.CacheOption = BitmapCacheOption.OnLoad;
        image.UriSource = new Uri(filepath);
        image.EndInit();


        PrintDialog printDialog = new PrintDialog
        {
            PrintQueue = printQueue
        };


        DrawingVisual visual = new DrawingVisual();
        var dc = visual.RenderOpen();
        dc.DrawImage(image, new System.Windows.Rect { Width = image.Width, Height = image.Height });

        printDialog.PrintVisual(visual, filepath);

    }

The printing itself works properly but it is just printing an blank piece of paper.

1 Answers

You need to call dc.Close() to flush the content to the DrawingVisual. Then it will have the content.

Related