查找 RichTextBox 中的所有图像

发布于 2024-10-07 06:12:19 字数 241 浏览 1 评论 0原文

我想与内嵌图像聊天。

richtextbox 很好,因为我可以在其中放置图像,但我想单独发送文本/图像。

  • 首先:发送文本(并在文本中放置图像占位符)。
  • 第二:发送图像并将其替换为占位符。

为此,我需要删除 Richtextbox 中的所有图像(并将它们单独发送)。 但我怎样才能找到这些图像呢?

顺便说一句:是否可以根据 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

写下不归期 2024-10-14 06:12:19

要查找RichTextBox中的所有图像,需要遍历所有Paragraphs及其Inlines;然后你可以对图像做任何你需要做的事情。例如,以下代码将增加 RichTextBox 内所有图像的大小(增加 1 像素)。

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

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;
                    }
                }
            }
        }
    }
}
维持三分热 2024-10-14 06:12:19

添加到 Prabu Arumugam 的答案中,Block 也可以是带有 ImageBlockUIContainer,因此您需要:

else if (block is BlockUIContainer)
{
    var container = (BlockUIContainer)block;
    if (container.Child is Image)
    {
        Image image = (Image)container.Child;
        // ...
    }
} 

我也更喜欢 Linq 语法为了紧凑性,也许是这样的:

public static void ResizeRtbImages(RichTextBox rtb)
{
    IEnumerable<Image> images = rtb.Document.Blocks.OfType<BlockUIContainer>()
            .Select(c => c.Child).OfType<Image>()
        .Union(rtb.Documents.Blocks.OfType<Paragraph>()
            .SelectMany(pg => pg.Inlines.OfType<InlineUIContainer>())
            .Select(inline => inline.Child).OfType<Image>()
        );
    foreach (var image in images)
    {
        // resize
    }
}

Adding to Prabu Arumugam's answer, the Block can also be a BlockUIContainer with an Image, so you would need:

else if (block is BlockUIContainer)
{
    var container = (BlockUIContainer)block;
    if (container.Child is Image)
    {
        Image image = (Image)container.Child;
        // ...
    }
} 

I'd also prefer Linq syntax for compactness, maybe something like this:

public static void ResizeRtbImages(RichTextBox rtb)
{
    IEnumerable<Image> images = rtb.Document.Blocks.OfType<BlockUIContainer>()
            .Select(c => c.Child).OfType<Image>()
        .Union(rtb.Documents.Blocks.OfType<Paragraph>()
            .SelectMany(pg => pg.Inlines.OfType<InlineUIContainer>())
            .Select(inline => inline.Child).OfType<Image>()
        );
    foreach (var image in images)
    {
        // resize
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文