Microsoft KB Archive/316563

= The Image.Save method does not save the file as the selected file type in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 316563

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005 Express Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q316563



SYMPTOMS
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.



CAUSE
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.



STATUS
This behavior is by design.



MORE INFORMATION
The GDI+ component of the .NET Framework has built-in encoders and decoders that support reading and writing the following file types:
 * BMP
 * GIF
 * JPEG
 * PNG
 * TIFF

GDI+ has additional built-in decoders that support read-only functionality for the following file types:
 * WMF
 * EMF
 * ICON

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 </li> On the View menu, click Designer to view the form designer.</li> Double-click the button control to insert the Click event handler for the button control.</li>  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 = &quot;C:\Windows\greenstone.bmp&quot;

'Replace the path below to the path of a WMF file to create Dim strDestFile As String = &quot;C:\Windows\greenstone.wmf&quot;

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 & &quot; loaded successfully.&quot;)

'Save the original image out as a Windows MetaFile Format file. imgOriginal.Save(strDestFile, ImageFormat.Wmf) MessageBox.Show(ControlChars.Quote & strDestFile & _         ControlChars.Quote & &quot; was saved successfully.&quot;)

'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 & &quot; loaded successfully.&quot;)

Catch excFileNotFound As System.IO.FileNotFoundException MessageBox.Show(ControlChars.Quote & strSourceFile & _         ControlChars.Quote & &quot; is not a valid path. &quot; & _          &quot;Please correct the code to use a valid path.&quot;) End Try

End Sub

</li> Replace the path of the strSourceFile variable with the path to an image file on your computer.</li> Replace the path of the strDestFile variable with the name of a WMF file to create on your computer.</li> Press F5 to compile and run the application.</li> Click the button.

You receive a message that indicates that the original image file was loaded successfully.</li> Click OK to close the message dialog box.

You receive a second message that indicates that the new image file was saved successfully.</li> Click OK.</li></ol>

Note that the code halts with the following exception:

An unhandled exception of type 'System.InvalidCastException' occurred in .exe

Additional information: Specified cast is not valid.

<div class="references_section">