Link to home
Start Free TrialLog in
Avatar of Anthar
Anthar

asked on

Extracting an image from a richtextbox into an image object

I am using a RichTextBox to enter information into a database.  The problem I have is that I need to store images seperately from the rest of the information.  I have determined what rtf represents the image, but I can't seem to convert it from the hex code that the rtf uses into any recognizable image format.  The image is in jpg format when it gets added to the RichTextBox.  Sample code would be great, but I'd even be happy with some kind of starting point.

Thanks
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

How are you converting the JPG image to RTF?

Bob
Avatar of Anthar
Anthar

ASKER

I'm just pasting it into the RichTextBox, the control does the conversion.  I can access the images source hex using the richtextbox.rtf property (the richtextbox.text property ignores the image altogether).  It seems like my problem might have been solvable with the richtextbox.oleobjects collection, if I were using VB6, but .NET doesn't have that functionality.

Thanks
I guess what I am to get at, so ineloquently, is where does the data come from that you paste into the RichTextBox?  Is it binary data, text data, etc.?

Bob
Avatar of Anthar

ASKER

I believe it is binary data, originally.  In this case, it's a jpg file from the local harddrive, retrieved via an OpenFileDialog, opened as a drawing.bitmap object, copied to the clipboard and pasted into the RichTextBox.  I've tried just translating the hex string into a binary string, but it hasn't worked.  I would post code, but it's in the office.  If you need it to help, let me know and I will get it and post it.

I found a similar problem (using C#) here on this site, but a solution was never posted, and eventually it was abandoned.  Here's the link to that post:

https://www.experts-exchange.com/questions/20960683/parse-richtext-image-into-image.html?query=parse+image&clearTAFilter=true

It was from a few months ago and I was hoping someone might have figured it out in the interim.
Do you mean that under VB6 you could do this image-extracting-job??

Anyway;... I found some information (also vb6) on rtf2html.

http://www2.bitstream.net/~bradyh/downloads/rtf2htmlrm.html

I have now too little time to figure some more out (too late,.. work tomorrow!),... but you said even a little could work out for you... I can imagine this vb6 code gives you some more about the formatting of RTF text with images etc...

If this code actually does anything... you have only to do a html->your_format conversion.

Anyways... good luck

Daniël
Avatar of Anthar

ASKER

Thanks for taking the time to look.  Unfortunately that code doesn't accept or deal with images at all.  

Otherwise it would be exactly what I need.
Sorry for the not useable webpage.

I'll give it another serious try:

http://www.microsoft.com/downloads/details.aspx?familyid=E5B8EBC2-6AD6-49F0-8C90-E4F763E3F04F&displaylang=en

Here you can find the "Rich Text Format Specification". On page 91 there is something about pictures etc... As fast as you can get it to a Bitmap-variabele you can output is however you want.


Use this serious article (http://www.developer.com/net/vb/article.php/10926_1576561_3) to get the idea how to start with reading RTF text from a RichTextBox.

Succes.
Avatar of Anthar

ASKER

I've looked at the RTF spec, it was helpful in isolating the rtf code that represents the image, but not in translating it back to usable format.  The second page is similar to the bitstream page.  Useful for other aspects of my project (converting rtf to html) but it doesn't deal with images either.

Thanks for taking the time to try again.

Finally,.. after so many hours...

(I hope this works for you....)

I couldn't resist the idea of not being able to filter images from a Richtextbox. I think this solution should work for u:

> The idea is to walk through your rtf-text in your box and scan for anything non-text, by selecting piece-by-piece the content of your box and then copy this 'non-text' to Clipboard

using this code:

------------------------------------------------------------------------------------------------------
        Dim n As Integer
        Dim text As String

        For n = 0 To RichTextBox1.TextLength

            'select piece by piece
            RichTextBox1.Select(n, 1)  

            'Check is this an Object
            If RichTextBox1.SelectionType = RichTextBoxSelectionTypes.Object Then
                'When found, copy this to Clipboard!
                RichTextBox1.Copy()
            End If
        Next
------------------------------------------------------------------------------------------------------

So now the picture is copied into clipboard... It now seems so easy... (not at all!!...)

> We now try to copy the image to a Picturebox so we can do whatever we like with the picture. Using the build-in Clipboard class doesn't work out because the data (in this case EnhMetaFile format) isn't retrieved at all... don't understand it the wrong way,.. it's there, but 'they' say there are problems between communication between programs and the clipboad... (Microsoft knows about this...) At least I tried anything.. but didn't work out... (maybe the problem is resolved in VB.net 2004 ;-) )

( Btw: it could be that if the image is stored on clipboad as bitmap, of tiff or so, you should be able to get it from clipboard using the .Net Class of Clipboard!! )

So we have to work around this... and of course:.. we need to use the windows API !


Add these references:

Imports System.Runtime.InteropServices
Imports System.Reflection


the code for the API: (put this between [Public Class Form1] and [end class] ):

------------------------------------------------------------------------------------------------------
    <DllImport("user32.dll", EntryPoint:="OpenClipboard", _
 SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
 Public Shared Function OpenClipboard(ByVal hWnd As IntPtr) As Boolean
    End Function

    <DllImport("user32.dll", EntryPoint:="EmptyClipboard", _
    SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function EmptyClipboard() As Boolean
    End Function

    <DllImport("user32.dll", EntryPoint:="SetClipboardData", _
    SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SetClipboardData(ByVal uFormat As Integer, ByVal hWnd As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll", EntryPoint:="CloseClipboard", _
    SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function CloseClipboard() As Boolean
    End Function

    <DllImport("user32.dll", EntryPoint:="GetClipboardData", _
    SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function GetClipboardData(ByVal uFormat As Integer) As IntPtr
    End Function

    <DllImport("user32.dll", EntryPoint:="IsClipboardFormatAvailable", _
    SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function IsClipboardFormatAvailable(ByVal uFormat As Integer) As Short
    End Function
------------------------------------------------------------------------------------------------------


... And code you should (e.g.) put behind a button_click:
------------------------------------------------------------------------------------------------------

        Const CF_ENHMETAFILE As Integer = 14
 
        Dim henmetafile As IntPtr
        Dim metaFile As System.Drawing.Imaging.Metafile

        If OpenClipboard(Me.Handle) Then
            If IsClipboardFormatAvailable(CF_ENHMETAFILE) <> 0 Then
                henmetafile = GetClipboardData(CF_ENHMETAFILE)
                metaFile = New Imaging.Metafile(henmetafile, True)

                PICTUREBOX.Image = metaFile

                CloseClipboard()
            End If
        End If
------------------------------------------------------------------------------------------------------


CAUTION!!!

I tried this with an rtf-document with a .GIF image. I created this RTF-file with Word (officeXP) and loaded it to a RichTextBox. It could be possible that if you paste a .BMP image into your box,, it is stored in CLIPBOARD as BITMAP. If that happens, you'll need to change the code to retrieve not the format EnhMetaFile but Bitmap or Tiff or so (look at for 'DataFormats')

Maybay I do a follow-up to this post to explain how to do other cases... but this should help you a lot I hope...

You should now be able to filter with a loop all the images (objects other than text) out of your RichTextBox, leaving the txt to be stored apart from the images...!

So far, succes, Daniël

btw: I remebered you especially asked for .jpg input into your Richtextbox to be handled... I tried the same code with a .jpg insert.. this worked for me too...

If you have serious trouble getting this code to run,.. I can post my vb.Net Solution in .zip format in my website.
Avatar of Anthar

ASKER

Great, I'll try this as soon as I get into the office tomorrow morning and let you know how it goes.

Thanks a lot.
ASKER CERTIFIED SOLUTION
Avatar of Daniellus83
Daniellus83
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial