Craig,
Unfortunately it's not quite that simple!
The NET equivalent of the StdPicture class (System.Drawing.Bitmap,
System.Drawing.Icon) are not directly compatible - you need to do the
conversion yourself using the OleCreatePictureIndirect API.
I created the following code to create and return a StdPicture containing a
bitmap; you should be able to adapt it to your needs.
<StructLayout(LayoutKind.Explicit)> _
Private Structure PICTDESC
' Methods
Public Sub New(ByVal hBitmap As IntPtr) : Me.New(hBitmap, IntPtr.Zero)
End Sub
Public Sub New(ByVal type As PICTYPE, ByVal handle As IntPtr) : Me.New(type,
handle, IntPtr.Zero, 0, 0)
End Sub
Public Sub New(ByVal hMetafile As IntPtr, ByVal x As Integer, ByVal y As
Integer) : Me.New(PICTYPE.PICTYPE_METAFILE, hMetafile, IntPtr.Zero, x, y)
End Sub
Private Sub New(ByVal type As PICTYPE, ByVal h1 As IntPtr, ByVal h2 As
IntPtr, ByVal x As Integer, ByVal y As Integer)
Me.cbSizeOfStruct = 0
Me.picType = type
Me.hBitmap = IntPtr.Zero
Me.hEnhMetaFile = IntPtr.Zero
Me.hIcon = IntPtr.Zero
Me.hMetafile = IntPtr.Zero
Me.hPalette = IntPtr.Zero
Me.xExt = 0
Me.yExt = 0
Me.cbSizeOfStruct = Marshal.SizeOf(Me)
Select Case type
Case PICTYPE.PICTYPE_BITMAP
Me.hBitmap = h1
Me.hPalette = h2
Return
Case PICTYPE.PICTYPE_METAFILE
Me.hMetafile = h1
Me.xExt = x
Me.yExt = y
Return
Case PICTYPE.PICTYPE_ICON
Me.hIcon = h1
Return
Case PICTYPE.PICTYPE_ENHMETAFILE
Me.hEnhMetaFile = h1
Return
End Select
Throw New NotSupportedException(String.Format("picType '{0}' is not
supported", Me.picType.ToString))
End Sub
' Fields
<MarshalAs(UnmanagedType.U4), FieldOffset(0)> _
Public cbSizeOfStruct As Integer
<FieldOffset(8)> _
Public hBitmap As IntPtr
<FieldOffset(8)> _
Public hEnhMetaFile As IntPtr
<FieldOffset(8)> _
Public hIcon As IntPtr
<FieldOffset(8)> _
Public hMetafile As IntPtr
<FieldOffset(12)> _
Public hPalette As IntPtr
<MarshalAs(UnmanagedType.U4), FieldOffset(4)> _
Public picType As PICTYPE
<FieldOffset(12)> _
Public xExt As Integer
<FieldOffset(16)> _
Public yExt As Integer
End Structure
Private Enum PICTYPE
' Fields
PICTYPE_BITMAP = 1
PICTYPE_ENHMETAFILE = 4
PICTYPE_ICON = 3
PICTYPE_METAFILE = 2
PICTYPE_NONE = 0
PICTYPE_UNINITIALIZED = -1
End Enum
<DllImport("OLEPRO32.DLL", PreserveSig:=False)> _
Private Shared Function OleCreatePictureIndirect(<In> ByRef pPictDesc As
PICTDESC, <In> ByRef riid As Guid, ByVal fOwn As Boolean) As
<MarshalAs(UnmanagedType.IUnknown)> Object
End Function
'*** Example code
Dim pictdesc1 As PICTDESC
Dim bitmap1 As Bitmap = Me.SignatureImage
If (bitmap1 Is Nothing) Then
Return Nothing
End If
Dim ptr1 As IntPtr = bitmap1.GetHbitmap
pictdesc1 = New PICTDESC(ptr1)
Dim type1 As Type = GetType(IPictureDisp)
Dim guid1 As Guid = type1.GUID
Return DirectCast(Signature.OleCreatePictureIndirect(pictdesc1,
guid1, True),IPictureDisp)
Hope this helps,
Nick Hall
> Hi
> I can confirm the error 'Specified cast is not valid' is arising when the
[quoted text clipped - 57 lines]
>>
>> Thanks in advance.
Craig - 26 Apr 2005 13:59 GMT
Hi Nick,
Thanks for the reply.
I'll let you know how I get on.
Craig
(It's not so easy migrating from VB6 to VB.NET after all)
> Craig,
>
[quoted text clipped - 171 lines]
> >>
> >> Thanks in advance.
Craig - 26 Apr 2005 18:23 GMT
Hi,
I'm glad to say, after a little more searching I found a simpler solution
(my VB.NET skills are not quite up to Nick's).
I have implemented a resource file and I have added a reference to the
Microsoft.VisualBasic.Compatibility assembly. It's the latter that provides
the cast capability (VB6.IconToIPicture). The Icon property now contains the
following code:-
Dim resMan As New
System.Resources.ResourceManager("MyAssembly.MyAssemblyResource",
Me.GetType.Assembly)
Dim resIcon As System.Drawing.Icon
resIcon = CType(resMan.GetObject("MyIcon"), System.Drawing.Icon)
Icon =
Microsoft.VisualBasic.Compatibility.VB6.IconToIPicture(resIcon)
resIcon.Dispose()
Regards
Craig
> Craig,
>
[quoted text clipped - 171 lines]
> >>
> >> Thanks in advance.