打印位图文件

seb*_*ler 4 c# printing wpf imaging c#-4.0

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

现在我想将Bitmap发送到缩放到打印机Pagesize的打印机.

我怎么能在WPF和C#中做到这一点.

如果您能提供一些代码或一点代码,我将是最棒的.

问候塞巴斯蒂安

Ars*_*yan 5

无需询问用户即可打印(打开打印对话框)

本文介绍如何使用对话框执行此操作

PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() == true)
{ dialog.PrintVisual(_PrintCanvas, "My Canvas"); }
Run Code Online (Sandbox Code Playgroud)

或者在你的情况下

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.");
}
Run Code Online (Sandbox Code Playgroud)