Visio Guy

Visio Discussions => Programming & Code => Topic started by: nashwaan on November 28, 2013, 01:29:36 PM

Title: [SOLVED] Convert stdole.IPictureDisplay of Shape.Picture to System.Drawing.Image
Post by: nashwaan on November 28, 2013, 01:29:36 PM
I am new to VB.NET.

I have a simple Visio add-in that has a TreeView1 control on it. I am trying to display the image of the shapes in the nodes of the TreeView.
For this, I have ImageList1 control but I am really stuck in the following:


TreeView1.ImageList = ImageList1
TreeView1.ImageList.Images.Add(shp.NameID, shp.Picture) ' for Add(key As String, image As System.Drawing.Image) ' ERROR HERE
TreeView1.Nodes.Add(shp.NameID, shp.Name, shp.NameID)   ' for Add(key As String, text As String, imageKey As String)


Ok, the type of shp.Picture is stdole.IPictureDisplay and the Add function of ImageList expects a parameter of type System.Drawing.Image but how to convert stdole.IPictureDisplay to System.Drawing.Image ? ? ?

I have used many codes posted on the internet without a success.

Does anybody knows how to do this. Please post the code (that really works please) rather than provide links to other sites.
I really would appreciate this help.

Yousuf
Title: Re: [HELP] Convert stdole.IPictureDisplay of Shape.Picture to System.Drawing.Image?
Post by: charlykuntz on November 28, 2013, 02:11:47 PM
Hello Yousuf,

maybe you can use the Shape.Export method to save the shape to an image file. Then you can use .NET's Image.FromFile() method to create an image.
Title: Re: [HELP] Convert stdole.IPictureDisplay of Shape.Picture to System.Drawing.Image?
Post by: nashwaan on November 28, 2013, 05:18:54 PM
Thanks for the suggestion  charlykuntz.

This approach is not feasible for the thing i want to achieve from the Add-in as this is VERY slow. There are thousands of shapes that i need to convert their images from stdole.IPictureDisplay of Shape.Picture to System.Drawing.Image.

Yousuf.
Title: Re: [HELP] Convert stdole.IPictureDisplay of Shape.Picture to System.Drawing.Image?
Post by: Nikolay on November 28, 2013, 06:42:52 PM
Hmmm... Haven't you suggested a solution two years ago?? :)

http://visguy.com/vgforum/index.php?topic=1987.msg11929#msg11929
Title: Re: [HELP] Convert stdole.IPictureDisplay of Shape.Picture to System.Drawing.Image?
Post by: nashwaan on November 28, 2013, 07:39:18 PM
Actually, in that post, I quoted JuneTheSecond's post which was based on your code!   ;D
Yes, I asked a similar question but it was for VBA not VB.NET.

Now that I converted your C# code to VB.NET code:


Dim picptr As IntPtr = New System.IntPtr(shp.Picture.Handle)
Dim img As System.Drawing.Image = New System.Drawing.Imaging.Metafile(picptr, True)
Dim bmp As System.Drawing.Bitmap = New System.Drawing.Bitmap(img)
TreeView1.ImageList.Images.Add(shp.NameID, bmp)

Note that i had to create an object for a pointer to shp.Picture.Handle as the direct cast (System.IntPtr)shp.Picture.Handle did not work.

and this Did solve my problem. Thanks a lot.

As usual, you are a star Nikolay.  8)
Title: Re: [SOLVED] Convert stdole.IPictureDisplay of Shape.Picture to System.Drawing.Image
Post by: qholmes on March 06, 2018, 05:43:49 PM
Hello,

I am working with a software developer on a Visio plugin. They generate an image of a shape as a reference but it seems that my Master shapes that have text in them... the text blows up and messes up the end result. I am trying to find out what method they are using to get the image. But does that ring any bells?

Thanks for any help.

Q