FlowDocument中丢失的图像另存为XPS文档 [英] Missing images in FlowDocument saved as XPS document

查看:183
本文介绍了FlowDocument中丢失的图像另存为XPS文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在获取FlowDocument中包含的图像以显示FlowDocument何时保存为XPS文档时,我遇到了一些困难.

I am having some difficulties getting images contained in a FlowDocument to show when the FlowDocument is saved as an XPS document.

这是我的工作:

  1. 使用创建图像WPF的图片控件.我将图像源设置为在对BeginInit/EndInit的调用中加上括号.
  2. 将图像添加到FlowDocument中,并以 BlockUIContainer .
  3. 使用
  1. Create an image using the Image control of WPF. I set the image source bracketed by calls to BeginInit/EndInit.
  2. Add the image to the FlowDocument wrapping it in a BlockUIContainer.
  3. Save the FlowDocument object to an XPS file using a modified version of this code.

如果我随后在XPS查看器中查看保存的文件,则不会显示该图像.问题在于,直到WPF在屏幕上实际显示图像之后,图像才会加载,因此它们不会保存到XPS文件中.因此,有一个解决方法:如果我首先使用 FlowDocumentPageViewer ,然后保存XPS文件,图像即被加载并显示在XPS文件中.即使FlowDocumentPageViewer被隐藏,此方法也有效.但这给了我另一个挑战.这是我想做的(用伪代码):

If I then view the saved file in the XPS viewer, the image is not shown. The problem is that the images are not loaded until actually shown on the screen by WPF so they are not saved to the XPS file. Hence, there is a workaround: If I first show the document on screen using the FlowDocumentPageViewer and then save the XPS file afterwards, the image is loaded and shows up in the XPS file. This works even if the FlowDocumentPageViewer is hidden. But that gives me another challenge. Here is what I wish to do (in pseudocode):

void SaveDocument()
{
    AddFlowDocumentToFlowDocumentPageViewer();
    SaveFlowDocumentToXpsFile();
}

这当然是行不通的,因为在将文档保存到XPS文件之前,FlowDocumentPageViewer从来没有机会显示其内容.我尝试将SaveFlowDocumentToXpsFile包装在对Dispatcher.BeginInvoke的调用中,但没有帮助.

This of course does not work since the FlowDocumentPageViewer never gets a chance to show its contents before the document is saved to the XPS file. I tried wrapping SaveFlowDocumentToXpsFile in a call to Dispatcher.BeginInvoke but it did not help.

我的问题是:

  1. 是否可以在不实际在屏幕上显示文档的情况下以某种方式强制图像在保存XPS文件之前加载? (我尝试摆弄
  1. Can I somehow force the images to load before saving the XPS file without actually showing the document on screen? (I tried fiddling with BitmapImage.CreateOptions with no luck).
  2. If there is no solution to question #1, is there a way to tell when FlowDocumentPageViewer has finished loading its contents so that I know when it is save to create the XPS file?

推荐答案

最终的解决方案与您使用的解决方案相同,即将文档放在查看器中并在屏幕上短暂显示.下面是我为此编写的帮助程序方法.

The eventual solution was the same as you came to, which is to put the document in a viewer and briefly show it on screen. Below is the helper method that I wrote to do this for me.

private static string ForceRenderFlowDocumentXaml = 
@"<Window xmlns=""http://schemas.microsoft.com/netfx/2007/xaml/presentation""
          xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
       <FlowDocumentScrollViewer Name=""viewer""/>
  </Window>";

public static void ForceRenderFlowDocument(FlowDocument document)
{
    using (var reader = new XmlTextReader(new StringReader(ForceRenderFlowDocumentXaml)))
    {
        Window window = XamlReader.Load(reader) as Window;
        FlowDocumentScrollViewer viewer = LogicalTreeHelper.FindLogicalNode(window, "viewer") as FlowDocumentScrollViewer;
        viewer.Document = document;
        // Show the window way off-screen
        window.WindowStartupLocation = WindowStartupLocation.Manual;
        window.Top = Int32.MaxValue;
        window.Left = Int32.MaxValue;
        window.ShowInTaskbar = false;
        window.Show();
        // Ensure that dispatcher has done the layout and render passes
        Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Loaded, new Action(() => {}));
        viewer.Document = null;
        window.Close();
    }
}

编辑:我刚刚在方法中添加了window.ShowInTaskbar = false,就好像您很快就能看到窗口出现在任务栏中一样.

I just added window.ShowInTaskbar = false to the method as if you were quick you could see the window appear in the taskbar.

用户永远不会看到"该窗口,因为该窗口位于屏幕上的Int32.MaxValue位置,这在早期的多媒体创作(例如Macromedia/Adob​​e Director)中很常见.

The user will never "see" the window as it is positioned way off-screen at Int32.MaxValue - a trick that was common back in the day with early multimedia authoring (e.g. Macromedia/Adobe Director).

对于正在搜索和发现此问题的人们,我可以告诉你没有其他方法来强制文档呈现.

For people searching and finding this question, I can tell you that there is no other way to force the document to render.

HTH,

这篇关于FlowDocument中丢失的图像另存为XPS文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆