all groups > dotnet drawing api > april 2008 >
You're in the

dotnet drawing api

group:

Text in Metafiles



Re: Text in Metafiles Michael Phillips, Jr.
4/17/2008 12:50:21 PM
dotnet drawing api: [quoted text, click to view]

Use Graphics.EnumerateMetafile to locate the record, then edit it and play
the edited record with Metafile.PlayRecord.

See MSDN for sample code:
http://msdn2.microsoft.com/en-us/library/system.drawing.imaging.metafile.playrecord.aspx



[quoted text, click to view]

Text in Metafiles elziko
4/17/2008 4:01:41 PM
Is there any way I can execute some sort of search and replace on the text
inside a metafile in code?

For example, if I had an instance of a Metafile I;d like to serach through
it looking for anu instances of the string "hello" and replace it with
"goodbye" before drawing the metafile onto a Graphics object.

It must leave all the other graphics in file file untouched including font
sizes etc.

Any ideas, help or pointers?

TIA

Re: Text in Metafiles Michael Phillips, Jr.
4/18/2008 11:42:04 AM
[quoted text, click to view]

Not without reviewing code that reproduces the problem.

My advice is to try recording an Emf format Enhanced Metafile rather than a
gdiplus EmfPlus or EmfPlusDual, etc.

Additionally, try examining known samples of Enhanced Metafiles by
enumerating the records for text.

You may be missing some needed records for setting up the records for
playback to your chosen device.

MSDN is loaded with samples and code for non gdiplus Enhanced Metafiles.

Re: Text in Metafiles elziko
4/18/2008 3:40:47 PM
Thanks for getting me started!

Originally I was just drawing my metafile straight onto my graphics object:

TheGraphics.DrawImage(TheMetaFile, 0,0)

Now I understand that I could replace the DrawImage method with a call to
EnumerateMetafile which uses the delegate mentioned in your link. So
initially, before trying to change any text I am just trying to get my
metafile to draw correctly without using the DrawImage method.

Using the code in your link I can get my metafile to be drawn. However, the
metafile contains one simple line and one simple string. When the PlayRecord
method gets called for each of these elements they are 'played' at vastly
different scales! The text appears roughly the correct size where as the
line is drawn at least one order of magnitude too large!

Do you have any ideas on why this may be happening?

TIA

[quoted text, click to view]

Re: Text in Metafiles Michael Phillips, Jr.
4/21/2008 11:37:04 AM
You should document this on Microsoft's Connect site.

After, you document it you can post a link here for us to vote.

Microsoft will only correct bugs and documentation errors when they are
notified and enough developers vote for the changes.

[quoted text, click to view]

Re: Text in Metafiles elziko
4/21/2008 2:32:58 PM
[quoted text, click to view]

I have been looking closely at the records in the files I am trying to
display. It seems to me that the code on the MSDN link you gave me has a bug
in it. As each record is enumerated the code elects not to play that record
if data <> IntPtr.Zero i.e. there is no data associated with that record.
The problem with this is that there seems to be records that contain no data
but are still required. In my case the MSDN code was not playing the
following records:

SetTextRenderingHint
SetAntiAliasMode
SetTextRenderingHint
SetAntiAliasMode
ResetWorldTransform
EndOfFile

So I changed the callback function on MSDN code to read:

Private Function MetafileCallback(ByVal recordType As EmfPlusRecordType,
ByVal flags As Integer, ByVal dataSize As Integer, ByVal data As IntPtr,
ByVal callbackData As PlayRecordCallback) As Boolean
Dim dataArray As Byte() = Nothing
If data <> IntPtr.Zero Then
' Copy the unmanaged record to a managed byte buffer
' that can be used by PlayRecord.
dataArray = New Byte(dataSize) {}
Marshal.Copy(data, dataArray, 0, dataSize)
End If
meta.PlayRecord(recordType, flags, dataSize, dataArray) 'note that
every record gets played even if the dataArray is empty
Return True
End Function

....and it now works fine - the only thing I find strange is that I have seen
the code in examples all over the internet and in one of our books here and
no-one else seems to have noticed the problem, or at least not mentioned it.

Anyway, if anyone else comes up with the same problem I hope this post
helps.

AddThis Social Bookmark Button