CopyEmfToClipboard and .NET

Hi,

 

i'm trying to make use of the CopyEmfToClipboard method of a Solid Edge Draft sheet object and then to save the enhanced metafile from the clipboard to a file in a different file format (i.e. TIFF).

 

Here is the core code doing this:

 

Const CF_ENHMETAFILE As Integer = 14

Dim henmetafile As IntPtr

Dim metaFile As System.Drawing.Image

 

If ClipBoardAPI.OpenClipboard(Me.Handle) Then

If ClipBoardAPI.IsClipboardFormatAvailable(CF_ENHMETAFILE) 0 Then

henmetafile = ClipBoardAPI.GetClipboardData(CF_ENHMETAFILE)

metaFile = New Metafile(henmetafile, True)

MessageBox.Show(metaFile.Size.ToString)

MessageBox.Show(metaFile.HorizontalResolution.ToString)

MessageBox.Show(metaFile.VerticalResolution.ToString)

With metaFile

.Save("d:\Temp\test.tiff", ImageFormat.Tiff)

End With

Else

MessageBox.Show("No EMF on clipboard")

End If

ClipBoardAPI.CloseClipboard()

End If

 

Where ClipBoardAPI is a static class to handle the clipboard, because .NET standard clipboard support doesn't recognize the Solid Edge emf object on the clipboard.

 

My questions/problems:

 

1) Saving the file results in an general GDI+ error and no file is written.

 

2) The resolution of the enhanced metafile seems to be hardcoded ~600 dpi. Is there any possibility to control the resolution?

 

Best regards,

 

Wolfgang

 

 

Posted by: Wolfgang Kunert
Post date: 10/31/2005 1:08:32 AM

25 REPLIES

RE: CopyEmfToClipboard and .NET

Hi Wolfgang,

 

I coded from scratch what you're trying to do and came to the same conclusion. GDI+ in .NET V1.1 has a lot of problems and you found one. I wish I could give you a reason why this doesn't work as it should, but I can't. The only hope that I can give you is that .NET V2.0 is coming and hopefully this kind of stuff works.

 

Regarding the resolution, I'm not sure. The only thing I saw in the CopyEMFToClipboard() is a parameter to specify AspectRatio. Not sure what impact that would have on DPI if any.

 

Sorry for all the bad news. Take care!

 

Posted by: Jason Newell
Post date: 10/31/2005 3:24:21 PM

RE: CopyEmfToClipboard and .NET

Hi Jason,

 

hmmm, that's bad news. Thanks a lot for your effort!

Aren't you using the CopyEmfToClipboard method for printing Solid Edge draft files? How did you manage parameters as resolution etc.?

 

Best regards,

 

Wolfgang

 

 

Posted by: Wolfgang Kunert
Post date: 10/31/2005 9:27:26 PM

RE: CopyEmfToClipboard and .NET

Wolfgang,

 

I have updated information for you if you're still interested in this topic. Let me know if you're still around and we can chat about it.

 

Posted by: Jason Newell
Post date: 7/5/2006 3:56:32 AM

RE: CopyEmfToClipboard and .NET

Hi Jason,

 

yes, i'm still interested in that topic.

Also under .NET 2.0 (with much enhanced clipboard handling) i observe the same problems. The copied EMF to clipboard is very often not found and/or processing crashes. Also the resolution issue is quite strange, if you i.e. try to convert the EMF to a TIF :-(

Sounds you have some good news??

 

Best regards

 

Wolfgang

 

Posted by: Wolfgang Kunert
Post date: 7/5/2006 4:19:48 AM

RE: CopyEmfToClipboard and .NET

Well I don't know if this will solve all of your problems be here's how I do things. Keep in mind that this is C++ converted code to .NET. When the .NET framework fails you, you can always revert back to the Win32 way of doing things. Not always as easy, but it usually works.

 

The attached .zip contains 1 solution with 2 projects. C# & VB.NET examples of converting from .dft -> .emf or .dft -> .tif.

 

Let me know how it goes.

 

Posted by: Jason Newell
Post date: 7/5/2006 6:10:17 AM

RE: CopyEmfToClipboard and .NET

Hi Jason,

 

thanks for the sample code!

It's very close to the way i made in my prototype testing too. I also successed to access the EMF from the clipboard by using the win32 API directly. But processing the EMF is still a challenge, because it requires huge amount of memory and time. I didn't experiment up to now with the complex EncoderParameters object possible when saving the EMF as TIF. This should allow compression and hopefully also controlling the resolution of the resulting TIF.

So far, using the viewer component to create a TIF via EMF instead of using Solid Edge and its PrintOut method, seems to me not to be much better concerning stability and the need of ressources. Another issue is getting properties and their linked property texts updated before creating the TIF.

 

Best regards,

 

Wolfgang

 

 

Posted by: Wolfgang Kunert
Post date: 7/6/2006 7:57:24 PM

RE: CopyEmfToClipboard and .NET

Hi Jason,

 

i could do some more testing and i must say that your clear way of accessing and processing the EMF works very reliable. I like your coding style every much. The code is also a good example on how to deal the correct way with WIN32 API calls in the .NET environment. Thanks again for your help and congratulations for your great engagement concerning programming Solid Edge and providing great templates in up to date coding style.

 

Best regards,

 

Wolfgang

 

 

Posted by: Wolfgang Kunert
Post date: 7/10/2006 10:12:38 PM

RE: CopyEmfToClipboard and .NET

Hi Wolfgang,

 

I have a application that use the CopyEmfToClipboard from the SE Viewer, but I don't use the .saveas function from .NET, but I, printout the clipboard result to a printer driver such as PDFCreator (http://sourceforge.net/projects/pdfcreator), this printer driver is free, and fully customizable with COM objects, and it's possible to control the resolution.

 

Best regards,

 

Aldo.

 

 

Posted by: Aldo Kruger
Post date: 7/11/2006 2:53:04 AM

RE: CopyEmfToClipboard and .NET

I've been doing a lot of research lately on this topic, for many reasons. 1st reason is that I'm working on a product that I plan on selling at some point. 2nd reason is there is a need at my company to improve Draft printing. Last night I figured out how to extract the EMF directly from the Draft file without any SE API.

 

I'm still in research mode but I definately have a proof of concept. This is exciting because now I can develop application that can quickly read the Draft, pull out the info that I'm interested in and do what I need. No more messing with CopyEMFToClipboard() and such. This also means my solution can work without Solid Edge or Solid Edge Viewer installed, which is what I'm targeting.

 

Fun stuff

 

Posted by: Jason Newell
Post date: 7/12/2006 2:46:26 AM