Error in IPictureDispToImage

Hi,
I have an application in VB which is bieng upgraded to VB.NET.
The application reads pictures and images from EXEs and Folders to place it in image lists. The aplication has a toolbar control onto which the contents of image lists are placed as icons.
The VB6 Code looked like
Tray.Icon = ImageList1.ListImages(imgIdx).Picture

frmTimer.Icons.ListImages.Add , icoKey & CStr(iName), m_cI(iName).IconPicture(Me.hdc, ClosestIndex(iName, 32))

The upgrade wizard changed the code to

Tray.Icon = VB6.IPictureDispToImage(ImageList1.ListImages(imgIdx).Picture)

frmTimer.DefInstance.Icons.ListImages.Add(, icoKey, VB6.ImageToIPictureDisp(m_cI(1).IconPicture(me.hdc, ClosestIndex(1, 32))))
I am getting 2 errors :
In first case it throws exception as "uknown image format"
In second case it throws exception as "Invalid cast"

Its also not possible to obtain the hdc value of a form in VB.NET. So me.hdc shows a compile time error.

Any suggetions?

[1095 byte] By [Debasish] at [2008-1-12]
# 1
Debasish wrote:

Hi,
I have an application in VB which is bieng upgraded to VB.NET.
The application reads pictures and images from EXEs and Folders to place it in image lists. The aplication has a toolbar control onto which the contents of image lists are placed as icons.
The VB6 Code looked like
Tray.Icon = ImageList1.ListImages(imgIdx).Picture

frmTimer.Icons.ListImages.Add , icoKey & CStr(iName), m_cI(iName).IconPicture(Me.hdc, ClosestIndex(iName, 32))

The upgrade wizard changed the code to

Tray.Icon = VB6.IPictureDispToImage(ImageList1.ListImages(imgIdx).Picture)

frmTimer.DefInstance.Icons.ListImages.Add(, icoKey, VB6.ImageToIPictureDisp(m_cI(1).IconPicture(me.hdc, ClosestIndex(1, 32))))
I am getting 2 errors :
In first case it throws exception as "uknown image format"
In second case it throws exception as "Invalid cast"

Its also not possible to obtain the hdc value of a form in VB.NET. So me.hdc shows a compile time error.

Any suggetions?


Debasish,

It might be possible that the VB6 runtime handles a format that .NET doesn't support (although unlikely). Is there anything special about the images that you are loading?

As for getting the hDC, you shouldn't be getting the hDC and then drawing on that, as a repaint event will wipe whatever you drew to it. Instead, you should attach to the Paint event, and handle your painting there (or override the OnPaint method).

Hope this helps.

- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

casperOne at 2007-8-21 > top of Msdn Tech,Visual Basic,Visual Basic Language...