试图从RichTextBlock复制/粘贴图像

本文关键字:图像 复制 RichTextBlock | 更新日期: 2023-09-27 18:08:54

我如何复制/粘贴在RichTextBlock(图像)?属性存在于一个RichTextBlock复制/粘贴?

下面是我正在创建的一个示例:

<RichTextBlock x:Name="txbTermsConditions" Height="Auto" Width="319" FontSize="16">
             <Paragraph>
                 //Example of xaml... the image is from file.xml
            </Paragraph> 
</RichTextBlock>

如果我从这段Xaml中选择所有的文本或图像并将其复制/粘贴到记事本中,我无法选择要复制/粘贴的文本或图像

试图从RichTextBlock复制/粘贴图像

试试下面的代码

      private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {   //click event
                    //MessageBox.Show("you got it!");
                    ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
                    MenuItem menuItem = new MenuItem("Cut");
                    menuItem.Click += new EventHandler(CutAction);
                    contextMenu.MenuItems.Add(menuItem);
                    menuItem = new MenuItem("Copy");
                    menuItem.Click += new EventHandler(CopyAction);
                    contextMenu.MenuItems.Add(menuItem);
                    menuItem = new MenuItem("Paste");
                    menuItem.Click += new EventHandler(PasteAction);
                    contextMenu.MenuItems.Add(menuItem);
                    richTextBox1.ContextMenu = contextMenu;
                }
            }
            void CutAction(object sender, EventArgs e)
            {
                richTextBox1.Cut();
            }
            void CopyAction(object sender, EventArgs e)
            {
                Graphics objGraphics;
                Clipboard.SetData(DataFormats.Rtf, richTextBox1.SelectedRtf);
                Clipboard.Clear();
            }
            void PasteAction(object sender, EventArgs e)
            {
                if (Clipboard.ContainsText(TextDataFormat.Rtf))
                {
                    richTextBox1.SelectedRtf
                        = Clipboard.GetData(DataFormats.Rtf).ToString();
                }
            } 

如果您想复制粘贴到其他应用程序,如记事本(仅文本而不是样式),请替换以下方法

void CopyAction(object sender, EventArgs e)
        {
            Clipboard.SetText(richTextBox1.SelectedText);
        }
        void PasteAction(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                richTextBox1.Text
                    += Clipboard.GetText(TextDataFormat.Text).ToString();
            }
        }