c# - 打印位图文件

标签 c# wpf c#-4.0 printing imaging

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

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

我如何在 WPF 和 C# 中执行此操作?

最佳答案

不询问用户就不能打印(打开打印对话框)

This article用对话框描述如何做到这一点

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.");
}

关于c# - 打印位图文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18202921/

相关文章:

c# - 将DataContext设置为StaticResource的属性

c# - Action<T> 等效于属性

c# - ItextSharp-如何为段落赋予颜色和粗体

c# - 项目无法编译

C#:子类列表 <Something>?

c# - "readonly"对接口(interface)中的 getter-only ref struct 属性有意义吗?

c# - 桌面 Java 应用程序替代方案

c# - 运行 WPF 应用程序的可执行文件时 log4net 不工作

c# - 为什么我通过 C# 代码得到格式错误的契约(Contract)?

asp.net - 如何从正在使用的REST API(ServiceStack)中获取URI