查找 FlowDocument 中的所有图像
由于我对 WPF FlowDocuments 还很陌生,我想问一下下面的代码是否正确。它应该将 FlowDocument 中包含的所有图像作为列表返回:
List<Image> FindAllImagesInParagraph(Paragraph paragraph)
{
List<Image> result = null;
foreach (var inline in paragraph.Inlines)
{
var inlineUIContainer = inline as InlineUIContainer;
if (inlineUIContainer != null)
{
var image = inlineUIContainer.Child as Image;
if (image != null)
{
if (result == null)
result = new List<Image>();
result.Add(image);
}
}
}
return result;
}
private List<Image> FindAllImagesInDocument(FlowDocument Document)
{
List<Image> result = new List<Image>();
foreach (var block in Document.Blocks)
{
if (block is Table)
{
var table = block as Table;
foreach (TableRowGroup rowGroup in table.RowGroups)
{
foreach (TableRow row in rowGroup.Rows)
{
foreach (TableCell cell in row.Cells)
{
foreach (var block2 in cell.Blocks)
{
if (block2 is Paragraph)
{
var paragraph = block2 as Paragraph;
var images = FindAllImagesInParagraph(paragraph);
if (images != null)
result.AddRange(images);
}
else if (block2 is BlockUIContainer)
{
var container = block as BlockUIContainer;
if (container.Child is Image)
{
var image = container.Child as Image;
result.Add(image);
}
}
}
}
}
}
}
else if (block is Paragraph)
{
var paragraph = block as Paragraph;
var images = FindAllImagesInParagraph(paragraph);
if (images != null)
result.AddRange(images);
}
else if (block is BlockUIContainer)
{
var container = block as BlockUIContainer;
if(container.Child is Image)
{
var image = container.Child as Image;
result.Add(image);
}
}
}
return result.Count > 0 ? result : null;
}
Since I am pretty new to WPF FlowDocuments I would like to ask if the code below is correct. It is supposed to return all Images contained in a FlowDocument as List:
List<Image> FindAllImagesInParagraph(Paragraph paragraph)
{
List<Image> result = null;
foreach (var inline in paragraph.Inlines)
{
var inlineUIContainer = inline as InlineUIContainer;
if (inlineUIContainer != null)
{
var image = inlineUIContainer.Child as Image;
if (image != null)
{
if (result == null)
result = new List<Image>();
result.Add(image);
}
}
}
return result;
}
private List<Image> FindAllImagesInDocument(FlowDocument Document)
{
List<Image> result = new List<Image>();
foreach (var block in Document.Blocks)
{
if (block is Table)
{
var table = block as Table;
foreach (TableRowGroup rowGroup in table.RowGroups)
{
foreach (TableRow row in rowGroup.Rows)
{
foreach (TableCell cell in row.Cells)
{
foreach (var block2 in cell.Blocks)
{
if (block2 is Paragraph)
{
var paragraph = block2 as Paragraph;
var images = FindAllImagesInParagraph(paragraph);
if (images != null)
result.AddRange(images);
}
else if (block2 is BlockUIContainer)
{
var container = block as BlockUIContainer;
if (container.Child is Image)
{
var image = container.Child as Image;
result.Add(image);
}
}
}
}
}
}
}
else if (block is Paragraph)
{
var paragraph = block as Paragraph;
var images = FindAllImagesInParagraph(paragraph);
if (images != null)
result.AddRange(images);
}
else if (block is BlockUIContainer)
{
var container = block as BlockUIContainer;
if(container.Child is Image)
{
var image = container.Child as Image;
result.Add(image);
}
}
}
return result.Count > 0 ? result : null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
LINQ 真是太神奇了:
LINQ is just freaking magical:
代码中几乎没有变化来添加
List
的覆盖范围Little change in Code to add coverage for
List
代码几乎没有变化,以增加 Figure 和 Floater 的覆盖范围
Little change in Code to add coverage for Figure and Floater