【问题标题】:Printing Image from Silverlight从 Silverlight 打印图像
【发布时间】:2014-04-11 02:08:52
【问题描述】:

我正在尝试在Silverlight 中以横向模式打印图像。

我找到了一个很好的例子here。大部分代码来自哪里。代码按预期完美运行。当我将 Line 更改为 Image 时,它​​失败了。

代码

       Canvas OuterCanvas = new Canvas();


       /* a container for everything that will print */

       Border OuterBorder = new Border()
       {

           BorderThickness = new Thickness(3),

           BorderBrush = new SolidColorBrush(Colors.Red),

           Margin = new Thickness(10)

       };

       double Width = e.PrintableArea.Width - OuterBorder.Margin.Left - OuterBorder.Margin.Right;

       double Height = e.PrintableArea.Height - OuterBorder.Margin.Top - OuterBorder.Margin.Bottom;


       /* NOTE: We're trying to force landscape, so swop the width and height */

       OuterBorder.Width = Height;

       OuterBorder.Height = Width;


       /* on portrait, this line goes down (leave the printer settings, we're trying to force landscape) */

       Line Line = new Line()
       {

           X1 = OuterBorder.Width / 2,

           Y1 = 0,

           X2 = OuterBorder.Width / 2,

           Y2 = OuterBorder.Height,

           Stroke = new SolidColorBrush(Colors.Blue),

           StrokeThickness = 3

       };


       //
       // Here is where I changed the Line to an Image
       //
       OuterBorder.Child = imageElementInXaml; //Line;

       OuterCanvas.Children.Add(OuterBorder);


       /* rotate 90 degrees, and move into place */

       var transformGroup = new TransformGroup();

       transformGroup.Children.Add(new RotateTransform() { Angle = 90 });

       transformGroup.Children.Add(new TranslateTransform() { X = e.PrintableArea.Width });

       OuterBorder.RenderTransform = transformGroup;

       e.PageVisual = OuterCanvas;

       e.HasMorePages = false;

我知道 Border 只能包含 1 个元素,我已经这样做了,当我自己打印图像而不尝试使其成为横向时,这也有效。那么,当我简单地将Line 替换为图像Element 时,为什么它不起作用

【问题讨论】:

  • 您是否在 XAML 中的图像有机会加载之前调用了此代码?
  • 否,因为图像已加载,并且上面的代码在按钮单击事件处理程序中
  • 您是否尝试过从头开始实例化和添加新图像,而不是使用 XAML 中的图像?
  • 是的.. 我做到了,我已经从另一个我发现的源更新了我的代码,这似乎工作正常。
  • 那么,为了将来这个问题的访问者参考,最终的解决方案是什么?顺便说一句,您可以将其发布为您自己问题的答案并将其标记为已回答,这是完全有效的。

标签: c# silverlight printing


【解决方案1】:

因此,自从发布此内容后,我发现了一些代码(现在不记得在哪里)帮助我进行打印。它不像我希望的那样干净,但它可以工作。

    void pd_PrintPage(object sender, PrintPageEventArgs e)
    {

        Image image = new Image();

        image.Source = imgPlayer.Source;


        //This is important
        image.Stretch = Stretch.Uniform;


        // Find the full size of the page
        Size pageSize =  new Size(e.PrintableArea.Width  + e.PageMargins.Left + e.PageMargins.Right, e.PrintableArea.Height + e.PageMargins.Top + e.PageMargins.Bottom);

        var MARGIN= 10;

        // Get additional margins to bring the total to MARGIN (= 96)
        Thickness additionalMargin = new Thickness
        {
            Left = Math.Max(0, MARGIN - e.PageMargins.Left),
            Top = Math.Max(0, MARGIN - e.PageMargins.Top),
            Right = Math.Max(0, MARGIN - e.PageMargins.Right),
            Bottom = Math.Max(0, MARGIN - e.PageMargins.Bottom)
        };


        // Find the area for display purposes
        Size displayArea = new Size(e.PrintableArea.Width - additionalMargin.Left - additionalMargin.Right,  e.PrintableArea.Height  - additionalMargin.Top - additionalMargin.Bottom);

        bool pageIsLandscape = displayArea.Width > displayArea.Height;
        bool imageIsLandscape = image.ActualWidth > image.ActualHeight;

        double displayAspectRatio = displayArea.Width / displayArea.Height;
        double imageAspectRatio = (double)image.ActualWidth / image.ActualHeight;

        double scaleX = Math.Min(1, imageAspectRatio / displayAspectRatio);
        double scaleY = Math.Min(1, displayAspectRatio / imageAspectRatio);



        // Calculate the transform matrix
        MatrixTransform transform = new MatrixTransform();

        if (pageIsLandscape == imageIsLandscape)
        {
        // Pure scaling
            transform.Matrix = new Matrix(scaleX, 0, 0, scaleY, 0, 0);
        }
        else
        {

            // Scaling with rotation
            scaleX *= pageIsLandscape ? displayAspectRatio : 1 /   displayAspectRatio;
            scaleY *= pageIsLandscape ? displayAspectRatio : 1 /   displayAspectRatio;

            transform.Matrix = new Matrix(0, scaleX, -scaleY, 0, 0, 0);
        }

        Image image2 = new Image
        {
            Source = image.Source,
            Stretch = Stretch.Fill,
            Width = displayArea.Width,
            Height = displayArea.Height,
            RenderTransform = transform,
            RenderTransformOrigin = new Point(0.5, 0.5),
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center,
            Margin = additionalMargin,
        };

        Border border = new Border
        {
            Child = image,
        };

        e.PageVisual = border;
    }

【讨论】:

  • 抱歉,最后一部分应该改为Border border= new Border { Child=image2}; 吗?否则我看不到任何创建 image2 的理由?
  • 是的,我认为你是对的,但在那种情况下,我还没有使用 Border border= new Border { Child=image2}; 进行测试:)
  • 因为如果目前的代码是正确的,并且删除实例化 image2 的代码块会使函数停止工作,那么这里的幕后会发生一些非常不同的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 2013-11-08
  • 1970-01-01
  • 2020-02-16
相关资源
最近更新 更多