When you use the
Image.Save method to save a graphic image as a Windows Metafile Format (WMF), Enhanced Metafile Format (EMF), or ICON file type, the resulting file is saved as a Portable Network Graphics (PNG) file instead.
This behavior occurs because the GDI+ component of the .NET Framework does not have an encoder that allows you to save files as WMF, EMF, or ICON files.
This behavior is by design.
The GDI+ component of the .NET Framework has built-in encoders and decoders that support reading and writing the following file types:
GDI+ has additional built-in decoders that support read-only functionality for the following file types:
Steps to Reproduce the Problem
- Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- Create a new Windows application in Visual Basic .NET or in Visual Basic 2005.
- Add a button control to the default form.
- Set the button properties as follows:
Name: Button1
Text: Save
- On the View menu, click Code to view the form's class module.
- Add the following statement to the top of the Code window, above the form's class definition:
Imports System.Drawing.Imaging
- On the View menu, click Designer to view the form designer.
- Double-click the button control to insert the Click event handler for the button control.
- Replace the Click event procedure with the following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Replace the path below to the path of an image on
'your computer.
Dim strSourceFile As String = "C:\Windows\greenstone.bmp"
'Replace the path below to the path of a WMF file to create
Dim strDestFile As String = "C:\Windows\greenstone.wmf"
Try
'Load the image using the FromFile method and store it in
'the imgOriginal variable.
Dim imgOriginal As Image = Image.FromFile(strSourceFile)
MessageBox.Show(ControlChars.Quote & strSourceFile & _
ControlChars.Quote & " loaded successfully.")
'Save the original image out as a Windows MetaFile Format file.
imgOriginal.Save(strDestFile, ImageFormat.Wmf)
MessageBox.Show(ControlChars.Quote & strDestFile & _
ControlChars.Quote & " was saved successfully.")
'Load the new image using the FromFile method and attempt
'to store it in a variable declared as MetaFile.
Dim wmfNew As Metafile = Image.FromFile(strDestFile) '<--Code fails here
MessageBox.Show(ControlChars.Quote & strDestFile & _
ControlChars.Quote & " loaded successfully.")
Catch excFileNotFound As System.IO.FileNotFoundException
MessageBox.Show(ControlChars.Quote & strSourceFile & _
ControlChars.Quote & " is not a valid path. " & _
"Please correct the code to use a valid path.")
End Try
End Sub
- Replace the path of the strSourceFile variable with the path to an image file on your computer.
- Replace the path of the strDestFile variable with the name of a WMF file to create on your computer.
- Press F5 to compile and run the application.
- Click the button.
You receive a message that indicates that the original image file was loaded successfully.
- Click OK to close the message dialog box.
You receive a second message that indicates that the new image file was saved successfully. - Click OK.
Note that the code halts with the following exception:
An unhandled exception of type 'System.InvalidCastException' occurred in application name.exe
Additional information: Specified cast is not valid.
For more information about using image encoders and decoders in Visual Studio .NET, browse to the following MSDN Web site: