在VS2003中,如果要给ActiveX控件的图象属性赋值,需要调用VB6.ImageToPicture。因为VS2003把图象属性的类型处理为stdole.IPictureDisp对象,所以需要利用这个函数将System.Drawing.Image转换为stdole.IPictureDisp。如:

        Dim bmp1 As New Bitmap(strm1)
        
Dim strm2 As System.IO.Stream = Me.GetType.Assembly.GetManifestResourceStream("PROP.CONSTRUC.bmp")
        ChkList1.set_ItemImage(ChkList1.NewIndex, VB6.ImageToPicture(bmp1))

如果在VS2005 Beta 2下运行上面的代码会导致一个System.InvalidCastException异常。提示信息为:
Unable to cast COM object of type 'System.Drawing.Image' to class type ''. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

不过这段信息并不能说明错误的真实原因。实际上这个异常是因为VS2005将ActiveX控件图象属性的类型处理为System.Drawing.Image,也就是说不再需要VB6.ImageToPicture的转换了。你可以将代码这样写:

        Dim bmp1 As New Bitmap(strm1)
        
Dim strm2 As System.IO.Stream = Me.GetType.Assembly.GetManifestResourceStream("PROP.CONSTRUC.bmp")
        ChkList1.set_ItemImage(ChkList1.NewIndex, bmp1)

posted on 2005-05-27 16:17  zealsoft  阅读(1524)  评论(0编辑  收藏  举报