How copy image from directory (folder) to RichTextBox in WPF?

本秂侑毒 提交于 2019-12-24 18:15:14

问题


I have RichTextBox in WPF. I can copy and paste (drag&drop) images from Web Sites and from Windows Photo Viewer .

But If i try to copy image from directory

I'll can't paste in my RichTextBox

But if I create a special button to paste image from directory it will work:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        string[] files = (string[])Clipboard.GetData(DataFormats.FileDrop);
        if (files != null && files.Length > 0)
        {
            foreach (var file in files)
            {
                // Filter out non-image files
                if (IsImageFile(file))
                {
                    BitmapImage bitmap = new BitmapImage(new Uri(file));
                    Image image = new Image();
                    image.Source = bitmap;
                    var container = new InlineUIContainer(image, rtbEditor.CaretPosition);
                    rtbEditor.CaretPosition = container.ElementEnd;
                    e.Handled = true;
                }
            }
        }
    }

来源:https://stackoverflow.com/questions/55441272/how-copy-image-from-directory-folder-to-richtextbox-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!