Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Convert IPictureDisp To System.Drawing.Image

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
marin_israel
10047 Views, 13 Replies

Convert IPictureDisp To System.Drawing.Image

Hi alll,

 

I have some annoying warning : 'Microsoft.VisualBasic.Compatibility.* classes are obsolete and supported within 32 bit processes only. To repair this, I search on this forum and I  found this link:

 

http://modthemachine.typepad.com/my_weblog/2012/02/bitmaps-without-vb6-icontoipicture.html

 

I fix 50% of my warning qith this class... but I want to display the thumbnail of my part on a form and this class do only ImageToIPictureDisp and not IPictureDispToImage.

 

Anyone have an idea to do this?

 

Thanks

Israel Marin

Sherbrooke,Qc

-------------------------------------
Israël Marin
Programmeur | Programmer
Logiflex
13 REPLIES 13
Message 2 of 14
JohanLarsson
in reply to: marin_israel

Is this of any help?

http://blogs.msdn.com/b/andreww/archive/2007/07/30/converting-between-ipicturedisp-and-system-drawin...

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
Tags (2)
Message 3 of 14
JohanLarsson
in reply to: JohanLarsson

This type of conversions are suitable to implement as Extension methods:

public static stdole.IPictureDisp ImageToPictureDisp(this Image image)

 

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
Message 4 of 14

on the basis of the comment of 

Message 5 of 14

Hi guys, thanks for your replies.

 

I checked your link, but I'm able to do this operation.

 

My question is only to stop having the warning :

"Microsoft.VisualBasic.Compatibility.* classes are obsolete and supported within 32 bit processes only"

 

Ths link I posted before on this topic is the answer but just for "ImageToIPictureDisp" but I have to do the inverse.

 

Thanks again!

Israel Marin

Sherbrooke, Qc, CAN

-------------------------------------
Israël Marin
Programmeur | Programmer
Logiflex
Message 6 of 14

for the warining "Microsoft.VisualBasic.Compatibility.* classes are obsolete and supported within 32 bit processes only", the MSDN link http://go.microsoft.com/fwlink/?linkid=160862' provides several solutions, you can fix this problem using anyone of them. (I copied them below for your convenience)

 

Actually the warning message only appears if the target version of the .NET framework is set to 4. So you must be using 4.0. Inventor 2012 is designed to use.net 3.5. I would suggest you to use .net 3.5 too if you are using Inventor 2012. Thus that warning will be gone and the old code works without problem.

 

As to the second solution, we also have a sample code for achieving conversion from Icon to IPictureDisp, this should work under .Net 4.0. The sample is enclosed in the bottom of this post. So you can use it to replace the Microsoft.VisualBasic.Compatibility.dll.

 

As to the third solution. It says you can just ignore that warning if you use VS2010 as that warning has no impact on your application, its title says "supported within 32 bit processes only" though.

 

Personally, I would suggest using the first solution.

 

 

Public NotInheritable Class PictureDispConverter
        <DllImport("OleAut32.dll", EntryPoint:="OleCreatePictureIndirect", ExactSpelling:=True, PreserveSig:=False)> _
        Private Shared Function OleCreatePictureIndirect( _
            <MarshalAs(UnmanagedType.AsAny)> ByVal picdesc As Object, _
            ByRef iid As Guid, _
            <MarshalAs(UnmanagedType.Bool)> ByVal fOwn As Boolean) As stdole.IPictureDisp
        End Function

        Shared iPictureDispGuid As Guid = GetType(stdole.IPictureDisp).GUID

        Private NotInheritable Class PICTDESC
            Private Sub New()
            End Sub

            'Picture Types
            Public Const PICTYPE_UNINITIALIZED As Short = -1
            Public Const PICTYPE_NONE As Short = 0
            Public Const PICTYPE_BITMAP As Short = 1
            Public Const PICTYPE_METAFILE As Short = 2
            Public Const PICTYPE_ICON As Short = 3
            Public Const PICTYPE_ENHMETAFILE As Short = 4

            <StructLayout(LayoutKind.Sequential)> _
            Public Class Icon
                Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Icon))
                Friend picType As Integer = PICTDESC.PICTYPE_ICON
                Friend hicon As IntPtr = IntPtr.Zero
                Friend unused1 As Integer
                Friend unused2 As Integer

                Friend Sub New(ByVal icon As System.Drawing.Icon)
                    Me.hicon = icon.ToBitmap().GetHicon()
                End Sub
            End Class

            <StructLayout(LayoutKind.Sequential)> _
            Public Class Bitmap
                Friend cbSizeOfStruct As Integer = Marshal.SizeOf(GetType(PICTDESC.Bitmap))
                Friend picType As Integer = PICTDESC.PICTYPE_BITMAP
                Friend hbitmap As IntPtr = IntPtr.Zero
                Friend hpal As IntPtr = IntPtr.Zero
                Friend unused As Integer

                Friend Sub New(ByVal bitmap As System.Drawing.Bitmap)
                    Me.hbitmap = bitmap.GetHbitmap()
                End Sub
            End Class
        End Class

        Public Shared Function ToIPictureDisp(ByVal icon As System.Drawing.Icon) As stdole.IPictureDisp
            Dim pictIcon As New PICTDESC.Icon(icon)
            Return OleCreatePictureIndirect(pictIcon, iPictureDispGuid, True)
        End Function

        Public Shared Function ToIPictureDisp(ByVal bmp As System.Drawing.Bitmap) As stdole.IPictureDisp
            Dim pictBmp As New PICTDESC.Bitmap(bmp)
            Return OleCreatePictureIndirect(pictBmp, iPictureDispGuid, True)
        End Function
    End Class

 

 

Message 7 of 14

Thanks for the 3 ways to fix the warning...

 

1-I use Inventor 2013, who supported only framework4.

 

2-As I say in previous post, I use this sample code but it's a one way converter. I can convert BMP to IPicture, but no, IPicture to BMP... because I want to show preview of ASM in a thumbnail.

 

3-I know I can ignore the warning but it's annoying. I have about 50 warnings...

 

 

 

 

-------------------------------------
Israël Marin
Programmeur | Programmer
Logiflex
Message 8 of 14

about converting IPicture to Bitmap, probably this link is helpful to you:

 

http://blogs.msdn.com/b/andreww/archive/2007/07/30/converting-between-ipicturedisp-and-system-drawin...

 

If you just want to use the ability directly without implementing yourself, you could use the InventorViewControlLib Inventor provides. The SDK sample is a demo.

  SDK\DeveloperTools\Samples\VCSharp.NET\Standalone Applications\ApprenticeServer\FileDisplay

Message 9 of 14

Thanks you Xiaodong!!

 

This link resolved my problem. With the AxHost, I can converted the Ipicture to Image!!

 

Israel Marin

Sherbrooke, Qc,CA

-------------------------------------
Israël Marin
Programmeur | Programmer
Logiflex
Message 10 of 14
Boorda
in reply to: xiaodong_liang

For those who are unfamiliar or do not program in C# here is the VB interpretation:

 

Imports System.Windows.Forms

Imports System.Drawing

Imports Inventor

 

''' <summary>

''' This class performs the following conversion:

'''     • Image to iPictureDisplay

'''     • iPictureDisplay to Image

''' </summary>

''' <remarks>

''' You must Import the following namespaces.

'''     • System.Drawing

'''     • System.Windows.Forms

'''

''' Note:

''' If you are using WPF then you will need to add a reference to the "System.Windows.Forms" framework to your project.

''' </remarks>

Public NotInheritable Class IPictureDispConverter : Inherits AxHost

 

    ''' <summary>

    ''' This method is required to impliment the AxHost.

    ''' You do not need to modify this sub.

    ''' </summary>

    Private Sub New()

        MyBase.New("")

    End Sub

 

    ''' <summary>

    ''' Converts a standard image to an IPictureDisp object.

    ''' </summary>

    ''' <param name="image">[Image] Image to convert to IPictureDisp</param>

    ''' <returns>[IPictureDisp] or Nothing on exception.</returns>

    Public Shared Function ImageToPictureDisp(image As Image) As IPictureDisp

        Try

            Return DirectCast(GetIPictureDispFromPicture(image), IPictureDisp)

        Catch ex As Exception

            'Add error message or logging here if desired.

            'Handle the conversion error by simply returning Nothing.

            Return Nothing

    End Try

    End Function

 

    ''' <summary>

    ''' Converts an IPictureDisp object to a standard image file.

    ''' </summary>

    ''' <param name="pictureDisp">[IPictureDisp] IPictureDisp object to convert to image.</param>

    ''' <returns>[Image] or Nothing on exception.</returns>

    Public Shared Function PictureDispToImage(pictureDisp As IPictureDisp) As Image

        Try

            Return GetPictureFromIPicture(pictureDisp)

        Catch ex As Exception

            'Add error message or logging here if desired.

            'Handle the conversion error by simply returning Nothing.

            Return Nothing

        End Try

    End Function

End Class

 

 

Below is a Visual Studio Item Template of the code.

 


Automation is key!
Message 11 of 14
MechMachineMan
in reply to: Boorda

So is it possible to easily use this code to make an exe I can use to export thumbnails as images (like bmp or jpg) so i can use them? 

 

I currently have a 64 bit computer, so I'm pretty sure I can't use it directly.... Then again, all the documentation about this conversion is all over the place for someone who is only a hobbyist programmer.

 

I tried importing it into visual studio express as a module and added the inventor reference, but I still have like 6 errors - no reference to axhost or image, mostly.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 12 of 14
Boorda
in reply to: MechMachineMan

The code above will work fine in 64 bit applications. I do it on 64 bit machines using WPF.

 

You'll need to create the code as a Class instead of a Module.

 

You can't inherit the AxHost in a module.

 

Inherit simply means you are merging all of an inheritable classes’ methods and properties into the current class. 

 

The "Public NotInheritable Class IPictureDispConverter : Inherits AxHost" line states that the IPictureDispConverter class inherits from the AxHost class which is in the System.Windows.Forms namespace.  If you are creating a WPF application then you will need to add a reference to the System.Windows.Forms namespace in your project.

 

Also the code should work fine in a 64 bit environment, but if not then it may be due to how your Configuration Manager is set up in your Visual Studio project; I usually target <Any CPU>. If you are still having issues after converting to a Class and adding the reference then I will explain in further detail.


Automation is key!
Tags (1)
Message 13 of 14
Boorda
in reply to: MechMachineMan

Also make sure you added a reference to the System.Windows.Drawing namespace in your project. that is where the Image object lives.

Automation is key!
Message 14 of 14
mikazakov
in reply to: MechMachineMan

This is an old problem. IPictureDisp can't do marshall to other exe application.

You should create macro

http://modthemachine.typepad.com/my_weblog/2010/06/accessing-thumbnail-images.html

and run the macro from your exe application.

But much better use AddIn. The AddIn have not problem with IPictureDisp.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report