簡體   English   中英

從Silverlight打印圖像

[英]Printing Image from Silverlight

我正在嘗試在Silverlight以橫向模式打印圖像。

我在這里找到了一個很好的例子。 大多數代碼來自哪里。 該代碼按預期工作完美。 當我將線更改為圖像時,它失敗了。

       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替換為image Element時,它為什么不起作用?

因此,自從發布此代碼以來,我發現了一些代碼(請記住現在的位置),這些代碼可以幫助我完成打印工作。 它不像我想要的那樣干凈,但是可以工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM