You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF XPS Document突然无法显示图片的问题及替代方案咨询

XPS预览图片不显示问题及替代方案咨询

问题背景与现象

这段代码已稳定运行多年,近期突然出现异常:

  • 通过WindowCommandCommandAction打开包含XamlPage的窗口时,TextBlock和图片均能正常显示;
  • 通过PreviewCommandAction生成XPS并在预览窗口打开时,仅显示TextBlock内容,图片完全不加载,无任何错误提示,Windows事件查看器也无相关日志。

关键代码片段

XamlPage(UserControl)

<StackPanel>
    <TextBlock Text="My Content"/>
    <Image Source="/Images/flower.jpg" Height="100"/>
</StackPanel>

Paginator(DocumentPaginator派生类)

public class Paginator : DocumentPaginator
{
    double pageHeight = 1122.52;
    double pageWidth = 793.7;
    Size mySize;

    public Paginator()
    {
        mySize = new Size(pageWidth, pageHeight);
    }

    
    public override DocumentPage GetPage(int pageNumber)
    {
        XamlPage page = new XamlPage();
        page.Measure(PageSize);
        page.Arrange(new Rect(new Point(0, 0), PageSize));
        page.UpdateLayout();
        return new DocumentPage(page, PageSize, new Rect(0, 0, 10, 10), new Rect());
    }

    public override bool IsPageCountValid
    {
        get { return true; }
    }

    public override int PageCount
    {
        get { return 1; }
    }

    public override Size PageSize
    {
        get { return mySize; }
        set { mySize = value; }
    }

    public override IDocumentPaginatorSource Source
    {
        get { return null; }
    }
}

MainVM中的命令实现

private void PreviewCommandAction()
{
    Paginator paginator = new Paginator();

    MemoryStream lMemoryStream = new MemoryStream();
    Package package = Package.Open(lMemoryStream, FileMode.Create);
    XpsDocument xpsDocument = new XpsDocument(package);
    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
    writer.Write(paginator);
    xpsDocument.Close();
    package.Close();

    Preview previewWindow = new Preview(lMemoryStream);

    previewWindow.ShowDialog();
}

private void WindowCommandCommandAction()
{
    XamlWindow xw = new XamlWindow();
    xw.Show();
}

Preview窗口XAML

<DocumentViewer Document="{Binding Document}"/>

PreviewVM代码

public class PreviewVM : ObservableObject
{
    private IDocumentPaginatorSource myDocument;
    private XpsDocument xps;
    private Uri packageUri;
    private Stream docStream;

    public PreviewVM(MemoryStream xpsStreamDocument)
    {
        docStream = xpsStreamDocument;
        Package package = Package.Open(docStream);

        // 创建XPS包的URI,仅作为PackageStore中的占位符
        string inMemoryPackageName = string.Format("memorystream://{0}.xps", Guid.NewGuid());
        packageUri = new Uri(inMemoryPackageName);

        // 将包添加到PackageStore
        PackageStore.AddPackage(packageUri, package);

        xps = new XpsDocument(package, CompressionOption.SuperFast, inMemoryPackageName);
        Document = xps.GetFixedDocumentSequence();
    }

    public IDocumentPaginatorSource Document
    {
        get { return myDocument; }
        set
        { 
            if (myDocument == value) return;
            myDocument = value;
            OnPropertyChanged(nameof(Document));
        }
    }
}

咨询问题

  1. 为何图片在XPS预览中突然无法显示?此前代码一直正常运行。
  2. 必须使用DocumentPaginator管理页面的前提下,还有哪些可行的预览方案?

内容的提问来源于stack exchange,提问作者Yannick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 23:15:29