0

FixedDocument通过添加FixedPages到创建一个PageContents,然后将它们添加到FixedDocument这样的

FixedDocument fd = new FixedDocument();
// add FixedPages in PageContent to fd

用 打印它们PrintDialog,像这样

pdialog.PrintDocument(fd.DocumentPaginator, "Test");

产生正确的页数。但是,打印的每一页(例如打印到 PDF)都是第一页的内容。

我尝试测试ImageSources我添加到的FixedPages,这些似乎是正确的。FixedDocument我也用DocumentViewer这样的方式测试了决赛

Window wnd = new Window();
DocumentViewer viewer = new DocumentViewer();
viewer.Document = fd;
wnd.Content = viewer;
try
{
    wnd.Show();
}
catch(Exception e)
{
    Console.WriteLine(e.ToString());
}

这奇怪地显示了我期望的正确输出。更奇怪的是我得到了一个IOExceptionafter wnd.Show();(这就是为什么我用 try/catch 包围它)。即使使用 try catch,我也只能在IOException我的MainWindow. 诸如“错误的用户名或密码”之类的东西 - 这没有意义,因为我要打印的图像是本地图像。

撇开DocumentViewer不谈,我Print()只打印第一页 n 次(n 是它应该是实际页数)的方法的问题仍然存在,只是认为其中的异常DocumentViewer可能会让某人了解潜在的问题。

这可能是FixedDocument 总是打印第一页的可能副本- 但是他没有提到问题DocumentViewer并且问题仍未得到解答。

提前感谢您的帮助!

4

2 回答 2

1

我遇到了类似的问题,从数据列表中的 FixedDocument 中打印标签,其中包含图像源列表(用户照片),并且还从用户 ID 的整数动态创建 QRCode 图像。

图像的格式是从我用来定位每个标签的文本字段和图像的自定义 UserControl 创建的。当我在 DocumentViewer 控件中查看创建的文档时,它显示得很完美。正确的照片图像,每个标签的正确 QRCode 图像。但是,当我打印文档(或保存为 PDF 文件或 XPS 文件)时,Ever Label 在标签上的照片和二维码图像位置都只有第一张图像。

当我看到这篇文章时,我想我会尝试保存然后按照建议重新加载图像,这很有效!然而,每页 30 个标签的 IO 开销以及许多标签页意味着这不是一个非常有用的解决方法!我

然后发现只需将 ImageSource 转换为 ByteArray,然后再返回,在添加到 FixedDocument 之前也可以工作,但不会增加 IO 开销。不是很优雅,但已经让我头疼了一个星期了!!

以下是构建标签的方法主体中的一段代码:

var qr = GetQRCodeImage(playerId);  // Gets ImageSource
var ph = LoadImage(data[dataIndex].Photo); // Gets ImageSource
var qrCode = FixDocumentCacheImageBugFix(qr); // Gets ImageSource
if (ph != null) {
    var photo = FixDocumentCacheImageBugFix(ph);
    label = new AveryBarcodeLabel(line1, line2, line3, qrCode, photo); // Calls constructor to instantiate new Label with new ImageSources
}
else {
    label = new AveryBarcodeLabel(line1, line2, line3, qrCode); // Calls constructor to instantiate new Label with new ImageSources (where photo is null)
}

这是我用来“修复”图像的方法

public static ImageSource FixDocumentCacheImageBugFix(ImageSource image) {
    var bytes = ImageSourceToBytes(image);
    return ByteToImage(bytes);
}
public static ImageSource ByteToImage(byte[] imageData) {
    var biImg = new BitmapImage();
    var ms = new MemoryStream(imageData);
    biImg.BeginInit();
    biImg.StreamSource = ms;
    biImg.EndInit();
    
    ImageSource imgSrc = biImg;

    return imgSrc;
}
public static byte[] ImageSourceToBytes(ImageSource imageSource) {
    byte[] bytes = null;
    var bitmapSource = imageSource as BitmapSource;

    if (bitmapSource != null) {
        var encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
        
        using (var stream = new MemoryStream()) {
                encoder.Save(stream);
                bytes = stream.ToArray();
            }
        }

        return bytes;
   }
于 2021-02-20T17:28:37.687 回答
0

So, this isn't really the answer to why it happened, but I found at least the culprit: my image.

I'm loading a multipage LZW-compressed TIFF like so:

TiffBitmapEncoder encoder = new TiffBitmapEncoder();
foreach (ImageSource frame in encoder.Frames)
{
    frame.Freeze();
    Images.Add(frame);
}

where Images is a collection of ImageSource. They display fine in the application, I can also save them again using a TiffBitmapEncoder, but printing them using WPF ends up with the in the question mentioned problem as well as - when using a DocumentViewer - an exception telling me about 'wrong username or password', which doesn't make sense.

The way I found out the image to be the problem was temporarily saving the individual ImageSources of the TIFF using a PngBitmapEncoder and immediately reloading the pages from the separate files with the same encoder into the same slot in my Images collection.

Since this works without any issues (no username/password exception in my DocumentViewer and my printing working correctly) I have to assume that he doesn't like something about the TIFF format.

This doesn't answer my underlying question of why it didn't work, but since this is at least a workaround that works, I'll just put that here and don't check the 'answered' mark just yet.

Maybe someone knows why my TIFF ImageSource produced those strange results?

于 2020-06-05T23:25:52.450 回答