显示窗体:
<Window x:Class="Jm.Ui.Assets.Print.PrintPreviewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Jm.Ui.Assets.Print"
mc:Ignorable="d"
Title="PrintPreviewWindow" Height="600" Width="800">
<Grid>
<DocumentViewer Name="documentViewer">
</DocumentViewer>
</Grid>
</Window>
MemoryStream ms = new MemoryStream();
Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
Uri DocumentUri = new Uri("pack://InMemoryDocument.xps");
PackageStore.RemovePackage(DocumentUri);
PackageStore.AddPackage(DocumentUri, package);
XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.Fast, DocumentUri.AbsoluteUri);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(((IDocumentPaginatorSource)flowDocument).DocumentPaginator);
documentViewer.Document = xpsDocument.GetFixedDocumentSequence();
xpsDocument.Close();
流文档模板
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:Jm.Ui.Assets.Print"
ColumnWidth="700" FontSize="14" FontFamily="宋体"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
TextOptions.TextFormattingMode="Display">
<Paragraph>
<Figure Width="160" Height="150" Padding="4" Margin="2" HorizontalAnchor="ContentCenter" VerticalAnchor="ContentTop">
<BlockUIContainer>
<Border SnapsToDevicePixels="True" BorderThickness="1" Padding="3" BorderBrush="#999">
<Image Source="D:\Download\logo.jpg"/>
</Border>
</BlockUIContainer>
</Figure>
</Paragraph>
</FlowDocument>
问题原因及解决办法见https://blog.csdn.net/quiteman/article/details/129359991
DocumentViewer 支持显示图像,在设置 DocumentViewer 的 Document 属性之前,你需要将文档存储为 XPS 格式。以下是应该添加到你的代码中的一些部分:
using (MemoryStream ms = new MemoryStream())
{
// 在内存中创建新的 Package
Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
// 在文档 URI 上移除包
Uri documentUri = new Uri("pack://InMemoryDocument.xps");
PackageStore.RemovePackage(documentUri);
// 向 Package 中添加文档并创建新的 XPS 文档
XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.Fast, documentUri.AbsoluteUri);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(((IDocumentPaginatorSource)flowDocument).DocumentPaginator);
// 将 XpsDocumentSequence 设置为 DocumentViewer 的 Document 属性
documentViewer.Document = xpsDocument.GetFixedDocumentSequence();
// 关闭 XpsDocument 和 Package,并释放资源
xpsDocument.Close();
package.Close();
}
注意:你需要导入 System.IO.Packaging
命名空间才能使用 Package 类。另外,文档 URI 必须以 "pack://"
开头。如果这些更改不起作用,那么检查一下 Image.Source
是否正确指向图片路径。
方案2。您需要将图片转换为 BitmapImage
并在 Image
上使用它。您可以使用以下代码将文件加载到 BitmapImage
中:
var image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(@"D:\Download\logo.jpg");
image.EndInit();
然后在您的 FlowDocument
中使用 Image
,并将 BitmapImage
设置为 Source
属性:
<Image Source="{Binding Image}" Width="160" Height="150" Padding="4" Margin="2" HorizontalAlignment="Center" VerticalAlignment="Top"/>
在代码中,您需要将 BitmapImage
作为 ImageSource
实例或 Binding
实例传递给 FlowDocument
。以下是您的示例代码的修改版:
MemoryStream ms = new MemoryStream();
Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
Uri DocumentUri = new Uri("pack://InMemoryDocument.xps");
PackageStore.RemovePackage(DocumentUri);
PackageStore.AddPackage(DocumentUri, package);
XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.Fast, DocumentUri.AbsoluteUri);
var image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(@"D:\Download\logo.jpg");
image.EndInit();
FlowDocument flowDocument = new FlowDocument();
flowDocument.Blocks.Add(new Paragraph(new InlineUIContainer(new Image { Source = image, Width = 160, Height = 150, Padding = new Thickness(4), Margin = new Thickness(2), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Top })));
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(((IDocumentPaginatorSource)flowDocument).DocumentPaginator);
documentViewer.Document = xpsDocument.GetFixedDocumentSequence();
xpsDocument.Close();
请注意,我已经将 Image
放到了 FlowDocument
中,并将其设置为 BitmapImage
的 Source
属性。这样,在 DocumentViewer
中就可以正常显示图片了。
@lanedm: 可以确定图片路径是正确的,按您说的方案2,也无法显示图片。
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:Jm.Ui.Assets.Print"
ColumnWidth="700" FontSize="14" FontFamily="宋体"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
TextOptions.TextFormattingMode="Display">
</FlowDocument>
using (MemoryStream ms = new MemoryStream())
{
Package package = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
Uri DocumentUri = new Uri("pack://InMemoryDocument.xps");
PackageStore.RemovePackage(DocumentUri);
PackageStore.AddPackage(DocumentUri, package);
XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.Fast, DocumentUri.AbsoluteUri);
var image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(@"D:\Download\logo.jpg");
image.EndInit();
FlowDocument flowDocument = new FlowDocument();
flowDocument.Blocks.Add(new Paragraph(new InlineUIContainer(new Image { Source = image, Width = 160, Height = 150, Margin = new Thickness(2), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Top })));
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
writer.Write(((IDocumentPaginatorSource)flowDocument).DocumentPaginator);
documentViewer.Document = xpsDocument.GetFixedDocumentSequence();
xpsDocument.Close();
package.Close();
}
@Mr-debug: 首先确认一下这个最简单的代码能否显示图片,再继续排查原因
<DocumentViewer>
<FlowDocument>
<Paragraph>
<Image Width="200" Height="200" Source="path/to/image.jpg" />
</Paragraph>
</FlowDocument>
</DocumentViewer>
@Mr-debug: 图片是可以显示的,其他贴子有讨论,仔细检查哪个代码或参数传递的不正确,或者先把自己的代码放一旁,直接用别人能运行的代码试试:https://www.coder.work/article/2982993
@lanedm:
@Mr-debug: 如果最简单的都报错,那就查wpf基础帮助文档看看,别人为什么可以
http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/FlowDocumentwithimages.htm
@lanedm: 虽您的解答非最佳答案,但仍感谢您的回答,谢谢!