views:

6346

answers:

6

Most of the examples I see say to put it on the clipboard and use paste, but that doesn't seem to be very good because it overwrites the clipboard.

I did see one method that manually put the image into the RTF using a pinvoke to convert the image to a wmf. Is this the best way? Is there any more straightforward thing I can do?

+6  A: 

There is a tutorial which shows how this can be done using C# here.

jheriko
Yes, this is the "one method that manually put the image into the RTF" that I mentioned in the question. (I added a link so it's clear that I've seen the codeproject article) I was hoping to do something less than the pinvoke :-(
Daniel LeCheminant
PInvoke is inevitable unless you wish to reverse engineer the creation of a legacy format (basic 1.0 WMF). That's what the P/Invoke is for.Again the article states that not doing this and dropping in more recent image formats works fine for advanced rtf readers (winword) but not the RichTextBox
ShuggyCoUk
+17  A: 

The most straightforward way would be to modify the RTF code to insert the picture yourself.

In RTF, a picture is defined like this:

'{' \pict (brdr? & shading? & picttype & pictsize & metafileinfo?) data '}' A question mark indicates the control word is optional. "data" is simply the content of the file in hex format. If you want to use binary, use the \bin control word.

For instance:

{\pict\pngblip\picw10449\pich3280\picwgoal5924\pichgoal1860 hex data}
{\pict\pngblip\picw10449\pich3280\picwgoal5924\pichgoal1860\bin binary data}

\pict = starts a picture group, \pngblip = png picture \picwX = width of the picture (X is the pixel value) \pichX = height of the picture \picwgoalX = desired width of the picture in twips

So, to insert a picture, you just need to open your picture, convert the data to hex, load these data into a string and add the RTF codes around it to define a RTF picture. Now, you have a self contained string with picture data which you can insert in the RTF code of a document. Don't forget the closing "}"

Next, you get the RTF code from your RichTextBox (rtbBox.Rtf), insert the picture at the proper location, and set the code of rtbBox.Rtf

One issue you may run into is that .NET RTB does not have a very good support of the RTF standard.

I have just made a small application* which allows you to quickly test some RTF code inside a RTB and see how it handles it. You can download it here: RTB tester (http://your-translations.com/toys).

You can paste some RTF content (from Word, for instance) into the left RTF box and click on the "Show RTF codes" to display the RTF codes in the right RTF box, or you can paste RTF code in the right RTB and click on "Apply RTF codes" to see the results on the left hand side.

You can of course edit the codes as you like, which makes it quite convenient for testing whether or not the RichTextBox supports the commands you need, or learn how to use the RTF control words.

You can download a full specification for RTF online.


NB It's just a little thing I slapped together in 5 minutes, so I didn't implement file open or save, drag and drop, or other civilized stuff.

Sylverdrag
I could not get RTB to show pngs, but at least it does show wmf.
Anton Tykhyy
+1  A: 

Here is what I do to hack the rich text control:

Insert the required image in wordpad or MS-WORD. Save the file as 'rtf'. Open the rtf file in notepad to see the raw rtf code. Copy the required tags & stuff to the 'rtf' property of your Rich Text Box (append to existing text).

There is some trial and error involved but works.

With C#, I use place holder StringBuilder objects with the necessary rtf code. Then I just append the image path.

This is a workaround for not having to learn the RTF syntax.

Sesh
That's the ugliest solution I've ever heard of.
lucifer
A: 

If you were in C++, the way to do it is thru OLE. More specifically, if you search Google for ImageDataObject it will show C++ code how to insert a HBITMAP into the RTF Control. One link is here.

Now, how this translates into .Net code, I don't know. I currently don't have the time to work thru the details.

Joel Lucsy
A: 
private void toolStripButton1_Click(object sender, EventArgs e)
    {
        FileDialog fDialog = new OpenFileDialog();
        fDialog.CheckFileExists = true;
        fDialog.CheckPathExists = true;
        fDialog.RestoreDirectory = true;
        fDialog.Title = "Choose file to import";
        if (fDialog.ShowDialog() == DialogResult.OK)
        {
            string lstrFile = fDialog.FileName;
            Bitmap myBitmap = new Bitmap(lstrFile);
            // Copy the bitmap to the clipboard.
            Clipboard.SetDataObject(myBitmap);
            DataFormats.Format format = DataFormats.GetFormat(DataFormats.Bitmap);
            // After verifying that the data can be pasted, paste
            if(top==true && this.rtTop.CanPaste(format))
            {
                rtTop.Paste(format);
            }
            if (btmLeft == true && this.rtBottomLeft.CanPaste(format))
            {
                rtBottomLeft.Paste(format);
            }
            if (btmCenter == true && this.rtBottomCenter.CanPaste(format))
            {
                rtBottomCenter.Paste(format);
            }
            if (btmRight == true && this.rtBottomRight.CanPaste(format))
            {
                rtBottomRight.Paste(format);
            }
        }
    }
A: 

I use the following code to first get the data from clipboard, save it in memory, set the image in clipboard, paste it in Rich Text Box and finally restore the data in Clipboard.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    OpenFileDialog1.Filter = "All files |*.*"
    OpenFileDialog1.Multiselect = True
    Dim orgdata = Clipboard.GetDataObject

    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        For Each fname As String In OpenFileDialog1.FileNames
            Dim img As Image = Image.FromFile(fname)
            Clipboard.SetImage(img)
            RichTextBox1.Paste()

        Next
    End If
    Clipboard.SetDataObject(orgdata)
End Sub

The OpenFileDailog1, RichTextBox1 and Button1 are Open File Dialog, Rich Text Box and button controls respectively.

Bibek Dahal