Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 625
user437899 Avatar asked Dec 22 '22 20:12

user437899


1 Answers

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;
                    }
                }
            }
        }
    }
}
like image 139
Prabu Arumugam Avatar answered Feb 01 '23 23:02

Prabu Arumugam