wpf 打印位图文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18202921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing an Bitmap File
提问by sebastianmehler
i wrote Code to Capture a Screenshot and save it to a Bitmap File in WPF.
我编写了捕获屏幕截图的代码并将其保存到 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#.
我怎样才能在 WPF 和 C# 中做到这一点。
If you could provide a hint or bit of Code I would be most Greatful.
如果您能提供一些提示或代码,我将是最伟大的。
Greetings Sebastian
问候塞巴斯蒂安
回答by Arsen Mkrtchyan
You can't print without asking user(opening print dialog)
不询问用户就无法打印(打开打印对话框)
This articledescribe 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.");
}

