Link to home
Start Free TrialLog in
Avatar of RMatzka
RMatzka

asked on

Convert Enhanced Metafile into RTF

Given a Windows handle to an Enhanced Metafile, how can I convert it into RTF code which displays this metafile, e.g. when it is handed over to the TextRTF property of a Rich Text Box control, or copied to Clipboard and pasted into Word? The RTF (V1.5) specification tells me to use the \pict and \emfblip control words, and to supply the picture data in hexadecimal format. The spec does not tell me how to get those picture data. For Bitmaps, the spec suggests to use the GDI "GetBitmapBits" function. By analogy, I thought I might use the GDI "GetEnhMetafileBits" function for an Enhanced Metafile. But it does not work. This is my RTF code:

{\rtf1\ansi\ansicpg1252\deff0\deflang1031
{\pict\emfblip\picw3620\pich2718\picwgoal2052\pichgoal1541
data as hexadecimal string
}
\par }

The Rich Text Box control can be made to paste Enhanced Metafiles (by pressing Ctl-v at runtime), so by inspecting its TextRTF property it is possible to see what code it creates. Unexpectedly, it uses the \wmetafile8 keyword instead of \emfblip, and it produces hexadecimal data which include the result of GetEnhMetafileBits (converted to hexadecimal string), but has additional bytes before and after those. I'm puzzled.

Can anybody help?
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

I don't understand, you already have proposed a possible solution. Paste the contents of clipboard to a RichtextBox control and save to a rtf file.
Avatar of RMatzka
RMatzka

ASKER

Yes, that's a workaround for my problem, but one with an unwanted side effect, since it affords to clear the clipboard, and the user of my app may well want to keep the clipboard contents for other purposes. Another workaround I thought of was to programmatically simulate OLE-Drag-And-Drop, but funny enough, the Rich Text Box control accepts EMF from the Clipboard, but not through OLE-Drag-And-Drop.
I need a clean solution, without side effects, and I can't think of anything else but directly create the necessary RTF code.
Note:
Points increased to 500 to replace the points from the other questions and fix the re-route problem to this question.

SpideyMod
Community Support Moderator @Experts Exchange
Well, could you send me an emf file to test with or where i could find one?
Avatar of RMatzka

ASKER

Having done some research, I understand the problem a bit better now. The documentation of the Rich Text Box control that comes with VB6 does not list the /emfblip control word among the supported control words, but does list /wmetafile. I.e., this control can embed Windows Metafiles (.wmf), but not Enhanced Metafiles (.emf). I have observed that whenever an EMF picture is copied to the clipboard, the clipboard offers both EMF and WMF format, when asked for available formats. So when I try to copy an EMF and paste it to the Rich Text control, what the latter does is use the WMF offered by the clipboard, and not my original EMF. And, if I try to use the /emfblip control word directly and feed it to the Rich Text Control, it simply won't recognize this control word.
In my case, the WMF format will not do, I need some of the EMF features. So, the issue for me boils down to these questions:
1. how to create valid RTF code containing a given EMF picture, so that it can be pasted e.g. into Word (I didn't manage that so far, having no more information than what's in the RTF 1.5 specification)
2. to find a better Rich Text control which does support EMF embedding.
Avatar of RMatzka

ASKER

Having done some research, I understand the problem a bit better now. The documentation of the Rich Text Box control that comes with VB6 does not list the /emfblip control word among the supported control words, but does list /wmetafile. I.e., this control can embed Windows Metafiles (.wmf), but not Enhanced Metafiles (.emf). I have observed that whenever an EMF picture is copied to the clipboard, the clipboard offers both EMF and WMF format, when asked for available formats. So when I try to copy an EMF and paste it to the Rich Text control, what the latter does is use the WMF offered by the clipboard, and not my original EMF. And, if I try to use the /emfblip control word directly and feed it to the Rich Text Control, it simply won't recognize this control word.
In my case, the WMF format will not do, I need some of the EMF features. So, the issue for me boils down to these questions:
1. how to create valid RTF code containing a given EMF picture, so that it can be pasted e.g. into Word (I didn't manage that so far, having no more information than what's in the RTF 1.5 specification)
2. to find a better Rich Text control which does support EMF embedding.
Avatar of RMatzka

ASKER

To be more specific, what I need is the Metafile description which can be set with CreateEnhMetaFile and be retrieved with GetEnhMetaFileDescription. I need this information to be preserved throughout the process of copying the EMF from my app to the clipboard, the user pasting it into her Word textstream, later the user copying it back from the Word textstream to the Clipboard, and my app getting it from the Clipboard.
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina 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