Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

wpf - Finding all Images in a FlowDocument

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...