Finding all Images in a FlowDocument

假如想象 提交于 2019-11-29 11:08:49

LINQ is just freaking magical:

public IEnumerable<Image> FindImages(FlowDocument document)
{
    return document.Blocks.SelectMany(FindImages);
}

public IEnumerable<Image> FindImages(Block block)
{
    if (block is Table)
    {
        return ((Table)block).RowGroups
            .SelectMany(x => x.Rows)
            .SelectMany(x => x.Cells)
            .SelectMany(x => x.Blocks)
            .SelectMany(FindImages);
    }
    if (block is Paragraph)
    {
        return ((Paragraph) block).Inlines
            .OfType<InlineUIContainer>()
            .Where(x => x.Child is Image)
            .Select(x => x.Child as Image);
    }
    if (block is BlockUIContainer)
    {
        Image i = ((BlockUIContainer) block).Child as Image;
        return i == null
                    ? new List<Image>()
                    : new List<Image>(new[] {i});
    }
    throw new InvalidOperationException("Unknown block type: " + block.GetType());
}
Deepak

Little change in Code to add coverage for List

    public static IEnumerable<Image> FindImages(FlowDocument document)
    {
        return document.Blocks.SelectMany(block => FindImages(block));
    }

    public static IEnumerable<Image> FindImages(Block block)
    {
        if (block is Table)
        {
            return ((Table)block).RowGroups
                .SelectMany(x => x.Rows)
                .SelectMany(x => x.Cells)
                .SelectMany(x => x.Blocks)
                .SelectMany(innerBlock => FindImages(innerBlock));
        }
        if (block is Paragraph)
        {
            return ((Paragraph)block).Inlines
                .OfType<InlineUIContainer>()
                .Where(x => x.Child is Image)
                .Select(x => x.Child as Image);
        }
        if (block is BlockUIContainer)
        {
            Image i = ((BlockUIContainer)block).Child as Image;
            return i == null
                        ? new List<Image>()
                        : new List<Image>(new[] { i });
        }
        if (block is List)
        {
            return ((List)block).ListItems.SelectMany(listItem => listItem
                                                                  .Blocks
                                                                  .SelectMany(innerBlock => FindImages(innerBlock)));
        }
        throw new InvalidOperationException("Unknown block type: " + block.GetType());
    }

static IEnumerable<T> GetElementsOfType<T>(DependencyObject parent) where T : class
{
   var childElements = LogicalTreeHelper.GetChildren(parent).OfType().ToList();
   return childElements.SelectMany(GetElementsOfType<T>).Union(childElements.OfType<T>());
}
....
var images = GetElementsOfType<Image>(document)

Little change in Code to add coverage for Figure and Floater

public static IEnumerable<Image> FindImages(FlowDocument document)
    {
        return document.Blocks.SelectMany(block => FindImages(block));
    }        

    public static IEnumerable<Image> FindImages(Block block)
    {
        if (block is Paragraph)
        {
            List<Image> toReturn = new List<Image>();
            var check = ((Paragraph)block).Inlines;
            foreach (var i in check)
            {
                if (i is InlineUIContainer)
                {
                    var inlineUiContainer = i as InlineUIContainer;
                    Image image = ((InlineUIContainer)inlineUiContainer).Child as Image;
                    if(image != null)
                    {
                        toReturn.Add(image);
                    }
                }
                else if (i is Figure)
                {
                    var figure = i as Figure;
                    var images = figure.Blocks.SelectMany(blocks => FindImages(blocks));
                    toReturn.AddRange(images);
                }
                else if (i is Floater)
                {
                    var floater = i as Floater;
                    var images = floater.Blocks.SelectMany(blocks => FindImages(blocks));
                    toReturn.AddRange(images);
                }
            }
            return toReturn;               
        }
        if (block is Table)
        {
            return ((Table)block).RowGroups
                .SelectMany(x => x.Rows)
                .SelectMany(x => x.Cells)
                .SelectMany(x => x.Blocks)
                .SelectMany(innerBlock => FindImages(innerBlock));
        }            
        if (block is BlockUIContainer)
        {
            Image i = ((BlockUIContainer)block).Child as Image;
            return i == null
                        ? new List<Image>()
                        : new List<Image>(new[] { i });
        }
        if (block is List)
        {
            return ((List)block).ListItems.SelectMany(listItem => listItem
                                                                  .Blocks
                                                                  .SelectMany(innerBlock => FindImages(innerBlock)));
        }
        throw new InvalidOperationException("Unknown block type: " + block.GetType());
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!