You are currently offline, waiting for your internet to reconnect

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

This article was previously published under Q316563
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:
  • 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

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Create a new Windows application in Visual Basic .NET or in Visual Basic 2005.
  3. Add a button control to the default form.
  4. Set the button properties as follows:
    Name: Button1
    Text: Save
  5. On the View menu, click Code to view the form's class module.
  6. Add the following statement to the top of the Code window, above the form's class definition:
    Imports System.Drawing.Imaging					
  7. On the View menu, click Designer to view the form designer.
  8. Double-click the button control to insert the Click event handler for the button control.
  9. 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 TryEnd Sub					

  10. Replace the path of the strSourceFile variable with the path to an image file on your computer.
  11. Replace the path of the strDestFile variable with the name of a WMF file to create on your computer.
  12. Press F5 to compile and run the application.
  13. Click the button.

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

    You receive a second message that indicates that the new image file was saved successfully.
  15. 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:

Article ID: 316563 - Last Review: 03/29/2007 10:01:26 - Revision: 2.2

  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • kbvs2005applies kbvs2005swept kbnofix kbprb KB316563