Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# - Find all images in a RichTextBox

I want a chat with inline images.

The richtextbox is good, because I can place images in it, but I want to send the text / images separate.

  • first: send the text (and place a image-placeholder in the text).
  • second: send the image and replace it with the placeholder.

For that I need to remove all images in the richtextbox (and send them separate). But how can I find the images?

And BTW: Is it possible to rescale the image dependent on the width of the richtextbox?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

To find all images in a RichTextBox, you need to traverse through all Paragraphs and its Inlines; and then you can do whatever you need with the image. For example, the following code will increase the size (by 1 pixel) of all images inside a RichTextBox.

public static void ResizeRtbImages(RichTextBox rtb)
{
    foreach (Block block in rtb.Blocks)
    {
        if (block is Paragraph)
        {
            Paragraph paragraph = (Paragraph)block;
            foreach (Inline inline in paragraph.Inlines)
            {
                if (inline is InlineUIContainer)
                {
                    InlineUIContainer uiContainer = (InlineUIContainer)inline;
                    if (uiContainer.Child is Image)
                    {
                        Image image = (Image)uiContainer.Child;
                        image.Width = image.ActualWidth + 1;
                        image.Height = image.ActualHeight + 1;
                    }
                }
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...