如何从Uri打印图像?

时间:2022-06-01 19:11:18

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:

我正在尝试打印我使用Uri对象引用的JPEG文件,并且遇到了一些困难。我发现当图像打印时,它会被轻微裁剪并翻转并镜像。我猜测裁剪是由于尺寸设置不正确但不知道为什么它被翻转和旋转造成的。假设这是一个自然的奇怪现象,我尝试通过对drawingContext对象应用转换来解决此问题,但这会导致打印空白页。这是我的代码:

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!

任何帮助将不胜感激 - 谢谢!

2 个解决方案

#1


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.

图像有点模糊,但肯定是可以接受的。我仍然不确定为什么我的图像需要翻转或镜像。

#2


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...

然后只打印Image对象,因为它派生自Visual ...您需要调用InvalidateVisual以便调用OnRender,如果不这样做会导致空白图像...

#1


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.

图像有点模糊,但肯定是可以接受的。我仍然不确定为什么我的图像需要翻转或镜像。

#2


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...

然后只打印Image对象,因为它派生自Visual ...您需要调用InvalidateVisual以便调用OnRender,如果不这样做会导致空白图像...