Converting Image to RTF picture

I want to create a function in an ActiveX dll that when passed a JPEG file, will convert the file to the RTF formatted picture (starting with "{\pict").

I want to be able to append truly embedded images to an rtf file.

I have been able to do this with a visual interfaced VB exe by loading a picture in the picture control and then copying the image to the clipboard and pasting to a richtextcontrol box .  This however is not a feasible or appropriate solution for an ActiveX dll.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

AzraSoundConnect With a Mentor Commented:
Well the clipboard technique was mentioned to be inappropriate because this is going to be a COM component used from ASP pages.  There is that slight chance that two people are requesting this document at the same time, but have two alternate images, so there could be a:

User1 pastes image to Clipboard
User2 pastes image to Clipboard
User1 embeds image from Clipboard to Richtextbox
User2 embeds image from Clipboard to Richtextbox

Thus, both users get User2's image.

I have written COM objects doing the exact same thing for use in ASP pages and have always opted for:

Call Richtextbox.OLEObjects.Add(, , strFile).DoVerb(-3)

but I am pretty sure you are limited to bitmaps there.  It would have been nice to see someone figure out what exactly goes on internally with the richtextbox and images.
I'd pay BIG money for this code too!!  (...listening...)
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Interesting... (I didn't realise RTFs could do this). I might be wondering down the completely wrong path here, but I tried the following...

1. Right clicked EE logo & saved it locally in GIF & BMP formats.

2. Copied EE logo onto clipboard, pasted it into WordPad & saved it into an RTF file.

3. A quick once over the RTF file in Notepad compared to the graphics formats under a hex editor reveals that the image data is held as an ASCII image of uncompressed binary data, very similar to a BMP file. It seems to be able to handle 8 and 24 bits-per-pixel images.

The "\wmetafile8" tag gives me the hunch that it's actually an ASCII windows metafile of a raster graphic. I seem to recall that there's several functions in the Win32 API for reading & writing WMF files: the following sites might give some clues...

Petzold's "Programming Windows" has a very accessible intro to these although all examples are given in C.

Even if the API has support for writing WMFs directly as ASCII values, you'll still have the little problem of decoding a JPEG file into an uncompressed bitmap. This is tough, I've found a C implementation at :

It might be simpler if you start by writing your control to handle .BMP and .PCX decoders first, they're a lot simpler internally. Alternatively there may be freeware ActiveX DLLs around which will do this for you.

Hope this is of some help.
I think you CAN have a form in your ActiveX DLL; just don't show it.
Richie_SimonettiIT OperationsCommented:
Well, to this time i got this (just an example).
they means (i think)....
{\pict\wmetafile8: always the same, start of image
\picw3968: width of image -1 in HIMETRIC units.
\pich1428: height of image -1 in HIMETRIC units.
\picwgoal2250: ? (maybe boundaries Width)
\pichgoal810:? (maybe boundaries Height)

After that comes "real" image in hex?
It appears like between hex values rtf adds some 0 ones, i am checking right know....
What i am tring to do is load image in binary format and append it to rtf in same way.
To get HIMETRIC values i am using an StdPicture object.
Keep rolling....
Good luck Richie...I played with this a few months ago and could never find out how to read the jpeg file's contents and convert them into the format they are stored in in the RTF fiel (I dont think its a straight read...more than likely, along the lines of what baldrick mentions above)
Richie_SimonettiIT OperationsCommented:
This is my analisis:
follow is a representation (in hex ) of first bytes of some image:


This is the same image but inside rtf document (some lines only):


As you could see, things starts to make sense at

You could see marks where things start to match....
Following on from my previous comment. You said up front:

[I have been able to do this with a visual interfaced VB exe by loading a picture in the picture control
and then copying the image to the clipboard and pasting to a richtextcontrol box .  This however is
not a feasible or appropriate solution for an ActiveX dll.]

Why is this not feasible? (Hide the form). Or appropriate?


Steve Mann
To make matters even worse the contents of the RTF will vary depending on colour depth. For example, an 8 bit-per-pixel bitmap can contain up to 256 colours, so it's most likely that raw palette information will also be tacked on at the end. In the quick (8 bpp) image test I last tried, the first 44 or so bytes of binary were unintelligible but were immediately followed by equivalent BMP data. I imagine those first 44 bytes are headers for width, height, colour depth, encoding method etc., and I still strongly suspect that Windows metafiles have something to do with it. If it *is* feasible to embed an undisplayed form in an ActiveX DLL (I can't imagine why not) then plonking a rich-textbox & picturebox in it for the conversions would dramatically simplify things.

Best of luck
wengierAuthor Commented:
The reason I said it was not feasible is that the Active X dll would be called from ASP.  The script could possible be called multiple times a second, by different users.  I beleive that the clipboard is a shared resource.  As such, it may be possible that copying and pasting into the clipboard by different instances of the dll could cause incorrect pictures to be pasted.  I might be very off base here. If so, then my initial solution may work.
Hi wengier,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: baldrick and manno

    *** clipboard (technique revisited by manno) will work:  OpenClipboard locks it, so just check to see if it is locked and sleep a while if so.
    *** baldrick clearly put some effort in here and desrves some vredit
    *** I am open to suggestions, also

wengier, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer
Thanks for the feedback AzraSound.
My thought was that once User1 had opened the clipboard, then when User2 tried to open it, the OpenClipboard call would fail.  So User2 could simply sleep for 100ms, then try again.  

I agree that the whole clipboard thing smells of kludge, but it seems like the only suggestion that is close to a working solution.  A very little research ought to resolve this:  If you can paste from the clipboard, you should be able to create the same object as a file (et al.) and insert it directly.  The alternative ideas of cracking the RTF coding is also viable, but looks like more work.  If this were a current question (it is 8 months old) and if it were posed in the C++ TA, and if it were worth 500 points, I'd be on it like stink on uhhh, a skunk.

-- Dan
[Un]fortunately, as you know, VB hides a lot of the grunt work regarding much Windows programming, including the clipboard.  In VB, there is no opening of the clipboard, closing it, etc.  You have a clipboard object and just say copy, paste, etc.  VB automatically does the opening and closing for you in that single function call.  Agreed, though, that this thread has probably received all the input it will get from those who have participated.
wengierAuthor Commented:
I have awarded the points to AzraSound because this is actually the solution that I came up with.  It is not perfect, but it works.  I have a component that I use to convert the jpeg file to a bmp file and then use Richtextbox.OLEObjects.Add to get the equivalent rtf information.  I parse out the actual image portion of the resultant rtf file in order to use this image in another rtf file.

Thanks to everyone for their input on this question.
Thanks wengler.  Keeping track of your open questions, and closing them to award points is a very good thing!

-- Dan
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.