在 Windows 8.1 下以横向模式剪切打印的 wpf 视觉对象
Posted
技术标签:
【中文标题】在 Windows 8.1 下以横向模式剪切打印的 wpf 视觉对象【英文标题】:Printed wpf visual is clipped in landscape mode under Windows 8.1 【发布时间】:2015-09-17 09:17:18 【问题描述】:我有一个简单的 wpf 桌面应用程序,它以横向模式打印位图。 在 Windows 8/8.1 下,打印输出被剪裁在页面底部,而在 Windows 7 下,打印输出正确。 代码很简单:加载位图,放入Image对象,测量可打印区域,排列图像并打印。
void printButton_Click(object sender, RoutedEventArgs e)
var pd = new PrintDialog();
if (!pd.ShowDialog().Value)
return;
pd.PrintTicket.PageOrientation = PageOrientation.Landscape;
pd.PrintTicket.PageBorderless = PageBorderless.None;
var printingCapabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri("D:\\printTest.bmp");
bitmapImage.EndInit();
var imageuiElement = new Image Source = bitmapImage ;
var desiredSize = new Size(printingCapabilities.PageImageableArea.ExtentWidth, printingCapabilities.PageImageableArea.ExtentHeight);
imageuiElement.Measure(desiredSize);
imageuiElement.Arrange(new Rect(new Point(printingCapabilities.PageImageableArea.OriginWidth, printingCapabilities.PageImageableArea.OriginHeight), imageuiElement.DesiredSize));
pd.PrintVisual(imageuiElement, "MyImage");
位图大小为 1518 x 1092 像素,96 DPI,即 40.2 x 28.9 cm。
我找到了问题Cannot print a document with landscape orientation under Windows 8 (WPF, .NET 4.0) 但对我的问题没有很好的回应(此外,我对作为横向打印本身没有问题)。 我已经用不同供应商的不同打印机对其进行了测试,所有打印输出都被剪裁了。软件 CutePDF writer 将其正确打印为 PDF。 任何帮助表示赞赏。
【问题讨论】:
【参考方案1】:Windows 8 似乎不会在其容器边界之外绘制 ui 元素。这就是打印输出被剪裁在底部的原因。 无论如何,我最终得到了这样的代码,它能够在横向页面上打印居中的位图:
printDialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
printDialog.PrintTicket.PageBorderless = PageBorderless.None;
var printingCapabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
if (printingCapabilities.PageImageableArea == null)
return;
var document = new FixedDocument();
document.DocumentPaginator.PageSize = new Size(printingCapabilities.PageImageableArea.ExtentWidth, printingCapabilities.PageImageableArea.ExtentHeight);
foreach (var imageStream in imageStreams)
document.Pages.Add(GeneratePageContent(imageStream, printingCapabilities, printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight));
try
printDialog.PrintDocument(document.DocumentPaginator, GlobalConstants.SoftwareName);
private PageContent GeneratePageContent(Stream imageStream, PrintCapabilities printingCapabilities, double paperWidth, double paperHeight)
imageStream.Seek(0, SeekOrigin.Begin);
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.StreamSource = imageStream;
bmp.EndInit();
var margin = new Thickness();
var pageSize = new Size();
if (printingCapabilities.PageImageableArea != null)
margin = new Thickness(
printingCapabilities.PageImageableArea.OriginWidth,
printingCapabilities.PageImageableArea.OriginHeight,
printingCapabilities.PageImageableArea.OriginWidth,
printingCapabilities.PageImageableArea.OriginHeight);
pageSize = new Size(printingCapabilities.PageImageableArea.ExtentWidth, printingCapabilities.PageImageableArea.ExtentHeight);
var imageUiElement = new Image
Source = bmp,
Margin = margin
;
var canvas = new Grid Width = paperWidth, Height = paperHeight ;
canvas.Children.Add(imageUiElement);
var fixedPage = new FixedPage
Width = paperWidth,
Height = paperHeight
;
fixedPage.Children.Add(canvas);
var pageContent = new PageContent();
((IAddChild)pageContent).AddChild(fixedPage);
pageContent.Measure(pageSize);
pageContent.Arrange(new Rect(new Point(), pageSize));
pageContent.UpdateLayout();
return pageContent;
【讨论】:
以上是关于在 Windows 8.1 下以横向模式剪切打印的 wpf 视觉对象的主要内容,如果未能解决你的问题,请参考以下文章