Pregunta

I wrote code to capture a screenshot and save it to a bitmap file in WPF.

Now I want to send the bitmap to a printer scaled to the pagesize of the printer.

How can I do this in WPF and C#?

¿Fue útil?

Solución

You can't print without asking user(opening print dialog)

This article describe how to do it with dialog

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

or in your case

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.");
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top