phi*_*dry 10 c# windows wpf xaml
我目前正在尝试使用PrintFixedDocument打印内容容器的内容(它只包含带有信息的数据网格)和图像.它可以完美地打印在我的机器(Windows 10)上,具有完整的图像质量,而另一台PC是Windows 8,质量是一样的.
但是,当在Windows 7 PC上完成时,图像质量变得非常差,最终结果非常模糊.这是一个问题,因为计算机由于各种原因无法从Windows 7更新,所以我想知道是否有其他人经历过这个,如果有的话有解决方法吗?也可能是我的GetFixedDocument方法的一个问题,虽然我无法弄清楚为什么这对win 10和8都有效,但不是7.
注意这是从每台PC上的应用程序的安装版本运行
还在所有3个操作系统上的多个打印机上进行了测试
任何帮助,将不胜感激
XAML:
<StackPanel Margin="-105,146,66,0" Height="900" VerticalAlignment="Top" x:Name="PrintImageContextMenu">
<Image Canvas.ZIndex="0" Source="{Binding Coupon.OverlayImagePath}" Margin="0,-21,-76,108" Stretch="Fill" />
<ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource DataViewerDataTemplateSelector}" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
C#:public partial class CouponViewerView {public CouponViewerView(){InitializeComponent(); }
public void Print()
{
//Executes On Thread
Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, (EventHandler)delegate
{
UpdateLayout();
var fixedDoc = PrintHelper.GetFixedDocument(StackPanelToPrint, new PrintDialog());
PrintHelper.ShowPrintPreview(fixedDoc);
}, null, null);
}
private void PrintCurrentForm(object sender, RoutedEventArgs e)
{
Print();
}
Run Code Online (Sandbox Code Playgroud)
C#打印助手代码:
public static void ShowPrintPreview(FixedDocument fixedDoc)
{
var wnd = new Window();
var viewer = new DocumentViewer();
viewer.Document = fixedDoc;
wnd.Content = viewer;
wnd.ShowDialog();
}
public static FixedDocument GetFixedDocument(FrameworkElement toPrint, PrintDialog printDialog)
{
var capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);
var pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
var visibleSize = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
var fixedDoc = new FixedDocument();
//If the toPrint visual is not displayed on screen we neeed to measure and arrange it
toPrint.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
toPrint.Arrange(new Rect(new Point(0, 0), toPrint.DesiredSize));
//
var size = toPrint.DesiredSize;
//Will assume for simplicity the control fits horizontally on the page
double yOffset = 0;
while (yOffset < size.Height)
{
var vb = new VisualBrush(toPrint)
{
Stretch = Stretch.None,
AlignmentX = AlignmentX.Left,
AlignmentY = AlignmentY.Top,
ViewboxUnits = BrushMappingMode.Absolute,
TileMode = TileMode.None,
Viewbox = new Rect(0, yOffset, visibleSize.Width, visibleSize.Height)
};
var pageContent = new PageContent();
var page = new FixedPage();
((IAddChild)pageContent).AddChild(page);
fixedDoc.Pages.Add(pageContent);
page.Width = pageSize.Width;
page.Height = pageSize.Height;
var canvas = new Canvas();
FixedPage.SetLeft(canvas, capabilities.PageImageableArea.OriginWidth);
FixedPage.SetTop(canvas, capabilities.PageImageableArea.OriginHeight);
canvas.Width = visibleSize.Width;
canvas.Height = visibleSize.Height;
canvas.Background = vb;
page.Children.Add(canvas);
yOffset += visibleSize.Height;
}
return fixedDoc;
}
Run Code Online (Sandbox Code Playgroud)
其他人也经历过这种情况,如果有的话有解决方法吗?
这是唯一可以直接回答的问题,是的,很多。请记住,您经常在打印机上大幅重新缩放图像,与显示器相比,它们是每英寸分辨率非常高的设备。启动Win7的机器通常以96dpi运行,后来的机器往往有更好的显示器,因此通常以更高的dpi设置运行。首先要注意的是源图像。如果它的像素大小适合 Win7 PC,那么当它放大到 600 dpi 时,它会变得非常模糊。
WPF 中最不直观的缩放行为可能是缩放后图像对齐与目标像素不完全匹配时发生的情况。这篇博文中详细描述了一个问题。请务必阅读此问题,它几乎完美适合您对 VisualBrush 的使用及其模糊问题。此问题已在 .NET 4.0 中通过添加的UseLayoutRounding属性得到解决。你没有使用它,你绝对应该使用它。不要盲目应用重复推荐的 BitmapScalingMode,图像类型(线条艺术与照片)非常重要。
| 归档时间: |
|
| 查看次数: |
891 次 |
| 最近记录: |