Command and Tool Bitmap property expects hWnd to display bitmaps


Summary
Using the Visual Basic Upgrade Wizard in Visual Studio to auto-translate Visual Basic 6 (VB6) code to Visual Basic .NET (VB.NET) is not 100% automated. This document will teach ArcGIS developers how to manually correct code where VB6 Command and Tool Bitmap Property values expect an hWnd to display Bitmaps but do not work after the auto-translation.

Example VB.NET Error:
Error 1 Reference to a non-shared member requires an object reference.
Error 2 'Handle' is not a member of 'System.Drawing.Image'.
Cause:
In VB6 the ICommand_Bitmap Property expects a windows handle (hWnd) to be passed back in order to display the Bitmap on the Button or Tools face of the GUI. In VB.NET in order to get an approximation for the hWnd and deal with the memory management of it appropriately a fair amount of code needs to occur. The process involves setting the hWnd (aka. a pointer) to a System.IntPtr. Then the user needs to explicitly release the memory to this pointer via a gdi32.dll API call to the DeleteObject Function, this is handled through the Finalize Sub when the class is destroyed. See the following code block for one implementation of this process.
Original VB6 code:
Private Property Get ICommand_Bitmap() As esriSystem.OLE_HANDLE
ICommand_Bitmap = frmResource.DateElementBitmap.Picture.Handle
End Property
[VB.NET]
'Initial auto-translated VB.NET conversion
Private ReadOnly Property ICommand_Bitmap() As System.Int32 Implements ESRI.ArcGIS.SystemUI.ICommand.Bitmap
Get
ICommand_Bitmap = frmResource.DateElementBitmap.Image.Handle
End Get
End Property
[VB.NET]
'Correct VB.NET translation

Dim m_hBitmap As System.IntPtr

Private ReadOnly Property ICommand_Bitmap() As System.Int32 Implements ESRI.ArcGIS.SystemUI.ICommand.Bitmap
Get
Dim frmResource As New frmResource
Dim bitmap As Bitmap = frmResource.DateElementBitmap.Image
bitmap.MakeTransparent(bitmap.GetPixel(0, 0))
m_hBitmap = bitmap.GetHbitmap()
Return CInt(m_hBitmap)
End Get
End Property

'Define the Windows gdi32.dll API call
<Runtime.InteropServices.DllImport("gdi32.dll")> _
                                   Private Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function


See Also:

Migrating from VB6 to VB.NET
General steps for migrating from VB6 to VB.NET




Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine