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:
{
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:
bool retry;
int retryCounter = 0;
do
{
retry = false;
pic = doc.Thumbnail;
System.Diagnostics.Debug.WriteLine("Handle = {0}", pic.Handle);
if (pic.Handle <= 0)
{
retry = true;
retryCounter++;
}
} while (retry);
System.Diagnostics.Debug.WriteLine("retryCounter = {0}", retryCounter);
System.Diagnostics.Debug.WriteLine("Got Thumbnail");
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:
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);