我在获取FlowDocument中包含的图像以显示FlowDocument何时保存为XPS文档时遇到一些困难。
这是我的工作:
如果然后在XPS查看器中查看保存的文件,则不会显示该图像。问题在于,直到WPF在屏幕上实际显示图像之后,图像才会加载,因此它们不会保存到XPS文件中。因此,有一个解决方法:如果我先使用FlowDocumentPageViewer在屏幕上显示文档,然后再保存XPS文件,则图像将被加载并显示在XPS文件中。即使FlowDocumentPageViewer被隐藏,此方法也有效。但这给了我另一个挑战。这是我想做的(用伪代码):
void SaveDocument()
{
AddFlowDocumentToFlowDocumentPageViewer();
SaveFlowDocumentToXpsFile();
}
这当然是行不通的,因为在将文档保存到XPS文件之前,FlowDocumentPageViewer从没有机会显示其内容。我尝试在对Dispatcher.BeginInvoke的调用中包装SaveFlowDocumentToXpsFile,但没有帮助。
我的问题是:
最终的解决方案与您所使用的相同,即将文档放在查看器中并在屏幕上短暂显示。下面是我为此编写的帮助程序方法。
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();
}
}
[Edit:我刚刚在方法中添加了window.ShowInTaskbar = false
,就好像您很快就能看到该窗口出现在任务栏中。
[在窗口位于Int32.MaxValue
时,用户将永远不会“看到”该窗口,这是早期多媒体创作(例如Macromedia / Adobe Director)在当今很常见的一种技巧。
对于正在寻找和发现这个问题的人,我可以告诉您没有其他方法强制文档呈现。
HTH,
耦合事物...您确定图像在写之前已调整大小吗?通常,您必须在控件上调用Measure,以便它可以相应地调整自身大小(无穷大可以使控件扩展为其宽度和高度)
image.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
而且,有时您必须碰碰UI线程,以便控件中的所有内容都得到更新
Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>{}));
您不必显示文档即可将图像保存到xps中。您是否在XpsSerializationManager上调用commit?
FlowDocument fd = new FlowDocument();
fd.Blocks.Add(new Paragraph(new Run("This is a test")));
string image = @"STRING_PATH";
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(image, UriKind.RelativeOrAbsolute);
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.EndInit();
MemoryStream ms = new MemoryStream();
Package pkg = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
Uri pkgUri = bi.UriSource;
PackageStore.AddPackage(pkgUri, pkg);
Image img = new Image();
img.Source = bi;
BlockUIContainer blkContainer = new BlockUIContainer(img);
fd.Blocks.Add(blkContainer);
DocumentPaginator paginator = ((IDocumentPaginatorSource)fd).DocumentPaginator;
using (XpsDocument xps = new XpsDocument(@"STRING PATH WHERE TO SAVE FILE", FileAccess.ReadWrite, CompressionOption.Maximum))
{
using (XpsSerializationManager serializer = new XpsSerializationManager(new XpsPackagingPolicy(xps), false))
{
serializer.SaveAsXaml(paginator);
serializer.Commit();
}
}
我能够通过将flowdocument放入查看器来解决此问题,然后进行度量/安排。
FlowDocumentScrollViewer flowDocumentScrollViewer = new FlowDocumentScrollViewer();
flowDocumentScrollViewer.Document = flowDocument;
flowDocumentScrollViewer.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
flowDocumentScrollViewer.Arrange(new Rect(new Point(0, 0), new Point(Double.MaxValue, Double.MaxValue)));