Click here to Skip to main content
15,564,323 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing an windows Application containing rich text box.In that i have to place images. I wrote code to place images inside rich text box.its working fine.But i want to drag an Image inside rich text box from one place to other in rich text box.I don't no how to do it. In internet also, i did not find any solution.So, Plz help me.
Updated 1-Nov-11 19:11pm
Sergey Alexandrovich Kryukov 1-Nov-11 11:46am    
Tag it! WPF, Silverlight, Forms, what?

To best of my knowledge, such operation is not supported by the RichTextBox classes (and I don't know what class do you mean exactly; please see my comment to the question: you should specify it exactly — always). So, the only way I can see is using clipboard.

In your DragDrop event handler you should do something like this:
myRichTextBox.DragDrop += (sender, eventArgs) => {
    RichTextBox richTextBox = (RichTextBox)sender;
    Image image = eventArgs.??? // depends on UI library
    //in WPF the declaration above would be BitmapSource image...
} //myRichTextBox.DragDrop

Again, it depends on what UI library you use (tag it — always!), but you should get the picture. :-)

Sorry the code above is not exact code you will need. This is because you did not specify exact class of RichTextBox and UI library. Please, based on this idea, go to MSDN help and find exact types and methods to use. With different .NET UI libraries, they are somewhat similar but different.


I recently found the solution for inserting images in System.Windows.Forms.RichTextBox; it is based on Clipboard. This is not a very good support, so I also offered an interesting alternative I tested and found great. Please see my recent answer:

how to open word file with images and bullets in a rich text box[^].

Share this answer
fjdiewornncalwe 1-Nov-11 12:27pm    
Agreed. It looks like this functionality would require the OP to do some serious user control coding extending the RTB.
Sergey Alexandrovich Kryukov 1-Nov-11 12:32pm    
Thank you, Marcus.

I can imagine such extension in System.Windows.Forms.RichTextBox by sub-classing this class and using raw Windows API, which will break code portability (will work only on Windows, while almost all System.Windows.Forms is ported to Mono and can be used on many OS). But I have no idea how to implement it on WPF. Difficult, to say the least. But clipboard-based work around will do the trick.
Sergey Alexandrovich Kryukov 5-Feb-12 2:02am    
Hi Marcus.

Thank you for supporting my opinion, but I recently tried to insert and image in RichTextBox and found it possible. So, I wasn't quite right and want to fix my mistake. I also added interesting alternative.

Please see my update, after [EDIT].
Try this code in constructor:

richTextBox.AddHandler(RichTextBox.DragOverEvent, new DragEventHandler(RichTextBox_DragOver), true);

richTextBox.AddHandler(RichTextBox.DropEvent, new DragEventHandler(RichTextBox_Drop), true);

Also set e.handled = false inside RichTextBox_DragOver
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900