You receive an "Image Cannot be Converted to Icon" compiler error message after you upgrade a Visual Basic 6.0 project to Visual Basic .NET or Visual Basic 2005 (311337)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q311337

SYMPTOMS

After you complete the steps in the Upgrade Wizard to upgrade a Visual Basic 6.0 project, when you try to run the newly created Visual Basic .NET or Visual Basic 2005 project, you receive the following compiler error message:
Value of type 'System.Drawing.Image' cannot be converted to 'System.Drawing.Icon'.
The Upgrade Wizard does not report any errors or warnings.

NOTE: The Visual Basic .NET Upgrade Wizard is included in Visual Studio .NET Professional.

CAUSE

This problem occurs because Visual Basic 6.0 code sets the Icon property of the form. In Visual Basic 6.0, the Icon property of a form is of type Image. In Visual Basic .NET or Visual Basic 2005, the Icon property is of type StdPicture.

The following line causes this error message in the Visual Basic .NET or Visual Basic 2005 project:
Me.Icon = Image1.Image
				
The following Visual Basic 6.0 code produces the above line:
Set Me.Icon = Image1.Picture
				

RESOLUTION

To resolve this problem, replace the following code in the upgraded Visual Basic .NET or Visual Basic 2005 project
Me.Icon = Image1.Image
				
with:
Dim bmp As Bitmap
bmp = Image1.Image
Me.Icon = Icon.FromHandle(bmp.GetHicon)
				

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Visual Basic 6.0 Standard EXE project. Form1 is created by default.

    Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

    For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site: For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:
  2. Add an Image control to Form1.
  3. Set the Picture property of the Image control to an icon (.ico) file.
  4. Insert the following code in the General Declarations section for Form1:
    Private Sub Form_Load()
        Set Me.Icon = Image1.Picture
    End Sub
    					
  5. Save the project.
  6. Start Visual Studio .NET or Visual Studio 2005, and open the Visual Basic 6.0 project that you saved in the last step. The Upgrade Wizard starts.
  7. Complete the steps in the Upgrade Wizard. The Upgrade Wizard creates a new Visual Basic .NET or Visual Studio 2005 project and an upgrade report. Make sure that the upgrade report does not report any errors or warnings.
  8. Press the F5 key to run the newly created Visual Basic .NET or Visual Basic 2005 project. You receive the error message that is listed in the "Symptoms" section.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbmigrate kbprb KB311337 kbAudDeveloper