如何从Uri打印图像?

时间:2009-06-18 15:58:03

标签: c# wpf printing

我正在尝试打印使用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);
    }
}

非常感谢任何帮助 - 谢谢!

2 个答案:

答案 0 :(得分:2)

这是我最终的结果:

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

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

答案 1 :(得分:0)

你可以这样做:

BitmapImage anImage = new BitmapImage(aUri);

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

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