简体   繁体   中英

How do I print an Image from a Uri?

I am attempting to print a JPEG file that I reference using a Uri object and am having some difficulties. I found that while the image was printing, it was cropped slightly and was flipped and mirrored. I'm guessing that the crop was caused by a size not being set properly but have no idea why it's being flipped and rotated. Assuming that this was a natural oddity, I attempted to resolve the issue by applying a transform to the drawingContext object but this results a blank page being printed. Here is my code:

public void Print(List<Uri> ListToBePrinted)
{
    XpsDocumentWriter writer = 
       PrintQueue.CreateXpsDocumentWriter(this.SelectedPrinter.PrintQueue);

    PrintCapabilities printerCapabilities = 
       this.SelectedPrinter.PrintQueue.GetPrintCapabilities();

    Size PageSize = 
       new Size(printerCapabilities.PageImageableArea.ExtentWidth,
                printerCapabilities.PageImageableArea.ExtentHeight);

    foreach (Uri aUri in ListToBePrinted)
    {
        BitmapImage anImage = new BitmapImage(aUri);

        //create new visual which would be initialized by image
        DrawingVisual drawingVisual = new DrawingVisual();

        //create a drawing context so that image can be rendered to print
        DrawingContext drawingContext = drawingVisual.RenderOpen();

        // Flips along X and Y axis (flips and mirrors)
        drawingContext.PushTransform(new ScaleTransform(-1, -1));

        drawingContext.DrawImage(anImage, new Rect(PageSize));

        drawingContext.Close();

        writer.Write(drawingVisual);
    }
}

Any help would be greatly appreciated - thank you!

Here's what I ended up with:

public void Print(List<Uri> ListToBePrinted)
{
    XpsDocumentWriter writer =
        PrintQueue.CreateXpsDocumentWriter(this.SelectedPrinter.PrintQueue);

    PrintCapabilities printerCapabilities =
        this.SelectedPrinter.PrintQueue.GetPrintCapabilities();

    Size PrintableImageSize =
        new Size(printerCapabilities.PageImageableArea.ExtentWidth,
                 printerCapabilities.PageImageableArea.ExtentHeight);

    foreach (Uri aUri in ListToBePrinted)
    {
        DrawingVisual drawVisual = new DrawingVisual();

        ImageBrush imageBrush = new ImageBrush();
        imageBrush.ImageSource = new BitmapImage(aUri);
        imageBrush.Stretch = Stretch.Fill;
        imageBrush.TileMode = TileMode.None;
        imageBrush.AlignmentX = AlignmentX.Center;
        imageBrush.AlignmentY = AlignmentY.Center;

        using (DrawingContext drawingContext = drawVisual.RenderOpen())
        {
            // Flips along X and Y axis (flips and mirrors)
            drawingContext.PushTransform(new ScaleTransform(-1, 1, PrintableImageSize.Width / 2, PrintableImageSize.Height / 2));
            drawingContext.PushTransform(new RotateTransform(180, PrintableImageSize.Width / 2, PrintableImageSize.Height / 2)); // Rotates 180 degree

            drawingContext.DrawRectangle(imageBrush, null, new Rect(25, -25, PrintableImageSize.Width, PrintableImageSize.Height));
        }

        writer.Write(drawVisual);
    }
}

The image is a little fuzzy but is certainly acceptable. I'm still not sure why my image needed to be flipped or mirrored.

Could you do something like:

BitmapImage anImage = new BitmapImage(aUri);

Image image = new Image();
image.BeginInit();
image.Source = anImage;
image.EndInit();
image.Measure(PageSize);
image.InvalidateVisual();

Then just print the Image object since it derives from Visual... You need to call InvalidateVisual so that OnRender will be called, if you didn't it would result in a blank image...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM