IPictureDisp.Handle might be negative

By Adam Nagy

As mentioned in this blog post as well, for some reason the Handle of the IPictureDisp returned by the Thumbnail property of ApprenticeServerDocument can sometimes be negative.

I tested this with my own app as well – the one referenced in this blog post.

Strangely, when my .NET app is compiled to 32 bit, it seems to cause no issue for the AxHostConverter.PictureDispToImage() converter method (see below) to turn the IPictureDisp into a System.Drawing.Image:

internal class AxHostConverter : AxHost {   private AxHostConverter() : base("") { }    static public stdole.IPictureDisp ImageToPictureDisp(Image image)   {     return (stdole.IPictureDisp)GetIPictureDispFromPicture(image);   }    static public Image PictureDispToImage(stdole.IPictureDisp pictureDisp)   {     return GetPictureFromIPicture(pictureDisp);   } }

However, when my .NET app is 64 bit then it is a problem and causes an exception. In that case the solution pointed out in the other blog post, i.e. accessing the Thumbnail multiple times, until the Handle of  IPictureDisp is positive solves the problem:

stdole.IPictureDisp pic = null; bool retry; int retryCounter = 0; do {   retry = false;   pic = doc.Thumbnail;   System.Diagnostics.Debug.WriteLine("Handle = {0}", pic.Handle);   if (pic.Handle

There is another way which seems even more reliable since it does not require accessing the Thumbnail multiple times. It's used in this blog post and inside my app it could be used like this:

stdole.IPictureDisp pic = doc.Thumbnail; Metafile mf = new Metafile(   new IntPtr(pic.Handle),   new WmfPlaceableFileHeader());  Image.GetThumbnailImageAbort callback =   new Image.GetThumbnailImageAbort(     delegate (){ return false; });  Image img = mf.GetThumbnailImage(   form1.pictureBox.Width,   form1.pictureBox.Height,   callback,    IntPtr.Zero);

Share this:

Like this:

Like Loading…

Comments

2 responses to “IPictureDisp.Handle might be negative”

  1. Thanks Mike! :)
    Added the info about it.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading