Question How To Copy/Paste Image From Resources To RichTextBox

in_valparaiso

Member
Joined
Apr 5, 2019
Messages
13
Programming Experience
10+
I have tried everything I know, I have searched for example code, I have Googled, and I got so desperate I even tried Bing, and simply cannot find a working example of how to take a picture in resources (I need to pass the name of the picture by variable, not hard coded) and paste it at the end of a RichTextBox.

Any insight would be greatly appreciated.
 
Use Clipboard.SetImage first, then RichTextBox methods Select to last position (TextLength) and Paste.
To get image by name look at the generated code for the resource property, because that is what it does too.
 
Kinda been to that point. Seems like whatever method I use to grab the image in Resources, it is the wrong format to paste into the richtextbox. I forget the error but usually something unable to use a Windows.Bitmap with Windows.Forms.Default.Format, or along those lines.
 
I'm not aware of a image format requirement, it works for me with a png image I added to resources just to test.
 
Here's how designer gets the image:
C#:
/// <summary>
///   Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap sample {
    get {
        object obj = ResourceManager.GetObject("sample", resourceCulture);
        return ((System.Drawing.Bitmap)(obj));
    }
}
I used that, but omitted the resourceCulture parameter since I didn't have any localization.
 
Use Clipboard.SetImage first, then RichTextBox methods Select to last position (TextLength) and Paste.
To get image by name look at the generated code for the resource property, because that is what it does too.
I can see that working, but won't that give a user a terrible user experience? Whatever they currently have in the clipboard will be replaced with the image you just put into the clipboard.
 
The more appropriate way is to use the IRichOle::InsertObject() method. When my co-workers and I were designing RichEdit 1.0, we designed this method in the original Win32 API RichEdit control as the way for the Win95 mail client as well as WordPad to be able to insert images and other COM objects (at that time it was called OLE rather than COM) into the RichEdit control.
 
I can see that working, but won't that give a user a terrible user experience? Whatever they currently have in the clipboard will be replaced with the image you just put into the clipboard.
It would be possible to replace the previous Clipboard contents afterwards. More trouble, yes, but transparent to the user.
 
Back
Top Bottom