Click here to Skip to main content
16,016,712 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btnPic1_Click(object sender, EventArgs e)
{
    Bitmap bitmap = ActiveWord.Properties.Resources.ah;
    Clipboard.SetImage(bitmap);
    richTextBox.Paste();
}


This code is taken from a project I am working on for college. Whenever I execute this piece of code, the image is not pasted into the richTextBox.

Any idea how I might be able to fix this?
Posted
Updated 19-Mar-12 8:40am
v3

1 solution

Basically, it should work. You need to run it under the Debugger. First, you need to makes sure bitmap really contains an image, and then that a clipboard contains the image &mdasj; simply open some other application which can past graphics and perform "Past" command while your execution is stopped at the breakpoint after the line with Clipboard.SetImage(bitmap). Second thing, you need to set insertion point for the image in the rich box. To do so, you need to use the properties SelectionStart and SelectionLength.

That's it.

Also, I would not advice to use RichBox control if you want to work with graphics. There are some different approached, much better then RTF. If you need an advice on it, you would need to tag the application type or UI library of your preference (what is that? Forms? WPF?) and explain your ultimate purposes.

—SA
 
Share this answer
 
Comments
[no name] 19-Mar-12 15:23pm    
How would I use the SelectionStart method?

This has no specific purpose. It is a project for college as I said and the instructions said I need to be able to insert an emoticon into a richTextBox.

SelectionStart? I told you, this is the property. See the MSDN help page on your control type. You did not tell us which one exactly. There are more the one type called "RichTextBox".

This will make a selection point at the very beginning of the document:
myRichTextBox.SelectionStart = 0;
myRichTextBox.SelectionLength = 0;

--SA
Sergey Alexandrovich Kryukov 19-Mar-12 15:44pm    
Everything basically has some purpose. OK, do you need it read-only or read write?
--SA
[no name] 19-Mar-12 15:28pm    
Btw, if I break the program after I add the image to the Clipboard and try to paste the Clipboard's content into Paint, it pastes the correct image (the one in bitmap in the above code).
Sergey Alexandrovich Kryukov 19-Mar-12 15:47pm    
OK, then the problem is the lines after the clipboard copy.

Again, RichTextBox is a bad thing if you need to do something like that.
--SA

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