Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Drawing / April 2008

Tip: Looking for answers? Try searching our database.

Text in Metafiles

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
elziko - 17 Apr 2008 16:01 GMT
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
Michael Phillips, Jr. - 17 Apr 2008 17:50 GMT
> 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.

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.playrec
ord.aspx


> Is there any way I can execute some sort of search and replace on the text
> inside a metafile in code?
[quoted text clipped - 9 lines]
>
> TIA
elziko - 18 Apr 2008 15:40 GMT
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

>> 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
[quoted text clipped - 19 lines]
>>
>> TIA
Michael Phillips, Jr. - 18 Apr 2008 16:42 GMT
> Do you have any ideas on why this may be happening?

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.
elziko - 21 Apr 2008 14:32 GMT
> You may be missing some needed records for setting up the records for
> playback to your chosen device.

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.
Michael Phillips, Jr. - 21 Apr 2008 16:37 GMT
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.

>> You may be missing some needed records for setting up the records for
>> playback to your chosen device.
[quoted text clipped - 38 lines]
> Anyway, if anyone else comes up with the same problem I hope this post
> helps.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.