在WPF中使用Microsoft Print To Pdf进行打印时将背景颜色更改为白色

时间:2018-02-28 10:45:15

标签: c# wpf

我正在使用Microsoft Print To Pdf 打印我的画布区域, Canvas 具有黑色背景

但是,我需要更改背景颜色

代码

public void RenderCanvasImage(int maxRight, int maxBottom, Canvas surface, Transform transform)
        {
            RenderTargetBitmap renderBitmap = new RenderTargetBitmap(maxRight, maxBottom,96d, 96d, PixelFormats.Pbgra32);
            renderBitmap.Render(surface);
            PrintDialog dialog = new PrintDialog();
            if (dialog.ShowDialog() != true)
            {
                return;
            }
            Grid grid = new Grid();
            Image myImage = new Image();
            myImage.Source = renderBitmap;
            myImage.Stretch = Stretch.Uniform;
            grid.Children.Add(myImage);
            Grid.SetColumn(myImage, 0);
            Grid.SetRow(myImage, 1);
            grid.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));
            grid.Arrange(new Rect(new Point(0, 0), grid.DesiredSize));
           if (dialog.PrintQueue.FullName == "Microsoft Print to PDF")
            {
                Microsoft.Win32.SaveFileDialog savedialog = new Microsoft.Win32.SaveFileDialog();
                savedialog.DefaultExt = "pdf";
                savedialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
                if (savedialog.ShowDialog() != true)
                {
                    return;
                }
                else
                {
                    if (savedialog.FileName.Contains(","))
                    {
                        MessageBox.Show("Comma(,) is not allowed in File Name/Directory.", "Message", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                        return;
                    }
                }
                System.Drawing.Bitmap bmp;
                using (System.IO.MemoryStream outStream = new System.IO.MemoryStream())
                {
                    BitmapEncoder enc = new BmpBitmapEncoder();
                    enc.Frames.Add(BitmapFrame.Create((BitmapSource)new WriteableBitmap(renderBitmap)));
                    enc.Save(outStream);
                    bmp = new System.Drawing.Bitmap(outStream);

                }

                System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument()
                {
                    PrinterSettings = new System.Drawing.Printing.PrinterSettings()
                    {
                        // set the printer to 'Microsoft Print to PDF'
                        PrinterName = "Microsoft Print to PDF",
                        // tell the object this document will print to file
                        PrintToFile = true,
                        // set the filename to whatever you like (full path)
                        PrintFileName = savedialog.FileName,

                    }
                };
                doc.PrintPage += (sender, args) =>
                {
                    args.Graphics.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, (int)grid.DesiredSize.Width, (int)grid.DesiredSize.Height));
                };
                doc.Print();
            }

        }

Kidnly提供了一些解决方案,因为之前我遇到的问题是,当在文件名或路径中使用逗号(,)时,Microsoft Print To Pdf将不会创建文件,因此在使用逗号时会出现用户错误我正在使用此代码。

0 个答案:

没有答案