user1893669
user1893669

Reputation:

Pasting image in RichTextBox

I have a RichTextBox and when I try to paste any image in it, I get an icon pasted instead of image itself. How can I get the image pasted in it?

Upvotes: 5

Views: 3596

Answers (1)

Sandy
Sandy

Reputation: 11687

I did it sometime ago. You have to place a Key Down event in richtextbox. Try this:

    private void RtbDocKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
        {
            DataFormats.Format df = DataFormats.GetFormat(DataFormats.Bitmap);
            StringCollection strcollect = Clipboard.GetFileDropList();

            Image image= Image.FromFile(strcollect[0]);
            Clipboard.Clear();
            Clipboard.SetImage(image);
            if (Clipboard.ContainsImage())
            {
                rtbBody.Paste(df);
                e.Handled = true;
                Clipboard.Clear();
            }
        }

Hope this helps.

Upvotes: 1

Related Questions