我编写了代码来捕获屏幕截图并将其保存到 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/