Click here to Skip to main content
15,881,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on a project involving Windows Enhanced Metafiles (.EMF) and am looking for some insight on the "logical coordinates" of text records withinin the file. My goal is to display the EMF file in a .NET control, then allow the user to select a rectangular portion of the image, then retrieve the text contained within the selection. As I understand it, this will involve translating the coordinates of the user's click within the device context displaying the file into the "logical coordinates" of the EMF text records. I'm enumerating the file and pulling the data out of the text records with this code:

C#
if (recordType == EmfPlusRecordType.EmfExtTextOutW)
{
    Marshal.Copy(data, dataArray, 0, dataSize);
    Int32 nChars = Marshal.ReadInt32(data, 36);          
    Int32 rclBounds_left = Marshal.ReadInt32(data, 0);
    Int32 rclBounds_top = Marshal.ReadInt32(data, 4);
    Int32 rclBounds_right = Marshal.ReadInt32(data, 8);
    Int32 rclBounds_bottom = Marshal.ReadInt32(data, 12);

    Int32 exScale = Marshal.ReadInt32(data, 20);
    Int32 eyScale = Marshal.ReadInt32(data, 24);

    Int32 ptlReferencex = Marshal.ReadInt32(data, 28);
    Int32 ptlReferencey = Marshal.ReadInt32(data, 32);

    Int32 rcl_left = Marshal.ReadInt32(data, 48);
    Int32 rcl_top = Marshal.ReadInt32(data, 52);
    Int32 rcl_right = Marshal.ReadInt32(data, 56);
    Int32 rcl_bottom = Marshal.ReadInt32(data, 60);

    System.Text.UnicodeEncoding TextUTF16 = new System.Text.UnicodeEncoding();
    dataString = TextUTF16.GetString(dataArray, Marshal.ReadInt32(data, 40) - 8, nChars * 2);
}


I am not too familiar with working with these types of files, but I believe I am pulling all of the data I need. However, I'm finding it difficult to find documentation to help me understand all of it thoroughly. Does anyone have any insight on how to convert the "logical coordinates" specified in this data into the coordinates of the device displaying the file? Any assistance would be greatly appreciated.
Posted

Metafile is pretty much perverted legacy data type; it it's pretty much obsolete already. You not going to believe that, but of you need to parse the metafile, you need to "play" it. Use:
http://msdn.microsoft.com/en-us/library/system.drawing.imaging.metafile.playrecord.aspx[^],
some code sample is here: http://msdn.microsoft.com/en-us/library/system.drawing.imaging.metafile.aspx[^].

The metafile is parsed by "playing", and it is created by "drawing". You can "draw" onto the metafile "surface" using an instance of System.Drawing.Graphics.
To solve all of your problem, you need to create some data presentation of your vector graphics based on GDI+ object, your own one. Then you parse an existing metafile into this data structure and work with it all the time. Then, if you need, you can "save" it in a metafile by "drawing" on it.

—SA
 
Share this answer
 
> I am working on a project involving Windows
> Enhanced Metafiles (.EMF) and am looking for some
> insight on the "logical coordinates" of text records
> withinin the file. My goal is to display the EMF file
> in a .NET control, then allow the user to select a
> rectangular portion of the image, then retrieve the text
> contained within the selection. As I understand it,
> this will involve translating the coordinates of the
> user's click within the device context displaying the
> file into the "logical coordinates" of the EMF text
> records. I'm enumerating the file and pulling the data
> out of the text records with this code:

If you are open to using a 3rd party .NET component, I think the MetaDraw .NET Winforms control will greatly simplify your task.

MetaDraw looks like a picturebox on your form but is specially designed for working with EMF and WMF images ( Note: EMF+ is NOT supported just plain EMF ) .

With MetaDraw - you'll
1) load your EMF image ( use LoadPicture method ),
2) and then set the EditMode property to put user into a selection mode. The user can then drag his mouse to draw out a rectangular selection region. All elements that are fully within that region will be selected, and the user will see selection markers to know which objects he / she has selected. The user can then also be allowed to add or remove objects from that selection.
3) You'll probably also want to set the EditFlags property to prevent the user from moving objects he has selected. The EditFlags property helps to customize the functionality enabled
in Selection edit mode.
4) You can then count the number of selected objects ( read ObjCount property ,
5) loop through them ( use ObjMove method)
6) and identify which are Text objects rather than shapes etc) ( check the ObjType property ),
7) and finally you'll read out the text for each of the selected text objects ( read the Text property ).

As you see you don't need to know anything about coordinates.
If you like MetaDraw will hand you back coordinates of each text object in the metafile, but you shouldn't even need to bother with that - you just want to know which text objects the user has selected.

By the way - all of this will work in a scrollable, zoomable window making it easier for the user to navigate around the image.

I hope this is helpful.
For more information on MetaDraw see - www.bennet-Tec.com

Please note - My company is the publisher of MetaDraw so this is not entirely unbiased - I do think this will meet your needs really quick and easy but I want you to know where I'm coming from up front.

Jeff
Bennet-Tec Information Systems


**
** Please include a copy of this message with any reply.
**

====================
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900