Author Topic: [SOLVED] Convert stdole.IPictureDisplay of Shape.Picture to System.Drawing.Image  (Read 807 times)

0 Members and 1 Guest are viewing this topic.

nashwaan

  • Full Member
  • ***
  • Posts: 83
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:

Code: [Select]
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
« Last Edit: November 28, 2013, 02:39:50 PM by nashwaan »
Give me six hours to chop down a tree and I will spend the first four sharpening the axe — Abraham Lincoln

Get Visio Guy's new book!


charlykuntz

  • Jr. Member
  • **
  • Posts: 68
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.

Visit the Visio Guy web site at www.visguy.com for hundreds more articles on Visio!


nashwaan

  • Full Member
  • ***
  • Posts: 83
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.
Give me six hours to chop down a tree and I will spend the first four sharpening the axe — Abraham Lincoln

Nikolay

  • Sr. Member
  • ****
  • Posts: 266
    • UnmanagedVisio
Hmmm... Haven't you suggested a solution two years ago?? :)

http://visguy.com/vgforum/index.php?topic=1987.msg11929#msg11929


nashwaan

  • Full Member
  • ***
  • Posts: 83
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:

Code: [Select]
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)
Give me six hours to chop down a tree and I will spend the first four sharpening the axe — Abraham Lincoln