打印位图文件

本文关键字:文件 位图 打印 | 更新日期: 2023-09-27 18:27:22

我编写了捕获屏幕截图的代码,并将其保存到WPF中的位图文件中。

现在我想将位图发送到一台按打印机页面大小缩放的打印机。

我如何在WPF和C#中做到这一点?

打印位图文件

您不能在不询问用户的情况下打印(打开打印对话框)

本文描述了如何使用对话框

PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() == true)
{ dialog.PrintVisual(_PrintCanvas, "My Canvas"); }

或者在您的情况下是

private void PrintSomethingNew()
{
  PrintDialog dialog = new PrintDialog();
  if (dialog.ShowDialog() != true)
  { return; }
  StackPanel myPanel = new StackPanel();
  myPanel.Margin = new Thickness(15);
  Image myImage = new Image();
  myImage.Width = 128;
  myImage.Stretch = Stretch.Uniform;
  myImage.Source = new BitmapImage(new Uri("C:''Tree.jpg", UriKind.Absolute));
  myPanel.Children.Add(myImage);
  TextBlock myBlock = new TextBlock();
  myBlock.Text = "A Great Image.";
  myBlock.TextAlignment = TextAlignment.Center;
  myPanel.Children.Add(myBlock);
  myPanel.Measure(new Size(dialog.PrintableAreaWidth,
    dialog.PrintableAreaHeight));
  myPanel.Arrange(new Rect(new Point(0, 0), 
    myPanel.DesiredSize));
  dialog.PrintVisual(myPanel, "A Great Image.");
}