c# – PrintFixedDocument wpf打印质量 – Windows 10/8与Windows 7

前端之家收集整理的这篇文章主要介绍了c# – PrintFixedDocument wpf打印质量 – Windows 10/8与Windows 7前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在尝试打印一个内容容器的内容(它只包含含有数据的信息)和一个使用PrintFixedDocument的图像.它完美无瑕地印在我的机器上( Windows 10),具有完整的图像质量,另一台是Windows 8,质量是一样的.

然而,当这样做在Windows 7 pc上时,图像质量变得非常差,最后的结果非常模糊.这是一个问题,因为计算机无法从Windows 7更新由于各种原因,所以我想知道是否有人经历过这一点,如果是有解决方法?也可能是我的GetFixedDocument方法的一个问题,虽然我无法解释为什么这将在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>

C#:
公共部分类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();
    }

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;
        }

解决方法

anyone else has experienced this and if so is there a workaround?

这是唯一直接负责的问题,是的,很多.请记住,您经常在打印机上大幅缩放图像,与显示器相比,它们是具有非常高的每英寸点数的设备.启动Win7的机器通常运行在96dpi,后来的机器往往具有更好的显示器,因此通常以较高的dpi设置运行.首先要注意的是源图像.如果它的像素尺寸足够适用于Win7 PC,那么当它被吹到600 dpi时它会变得非常模糊.

可能在WPF中最不直观的缩放行为是当缩放后图像对齐不完全匹配目标像素时会发生什么.在this blog post中描述的问题很好.请务必阅读this SO question,几乎完美适合您使用VisualBrush及其模糊性问题.这个问题在.NET 4.0中被添加UseLayoutRounding属性.你没有使用它,你绝对应该.不要盲目地应用dup建议的BitmapScalingMode,图像类型(线条与照片)很重要.

猜你在找的C#相关文章