查找 FlowDocument 中的所有图像

发布于 2024-09-26 19:46:03 字数 2739 浏览 4 评论 0原文

由于我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趴在窗边数星星i 2024-10-03 19:46:03

LINQ 真是太神奇了:

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());
}

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());
}
善良天后 2024-10-03 19:46:03

代码中几乎没有变化来添加 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());
    }

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());
    }
笨死的猪 2024-10-03 19:46:03

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)

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)
っ〆星空下的拥抱 2024-10-03 19:46:03

代码几乎没有变化,以增加 FigureFloater 的覆盖范围

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());
    }

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());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文