By Wayne Brill
You can get the thumbnail of a document using the ThumbNail property of a Document. This property returns an IPictureDisp. To show the Thumbnail you may need to convert it to a System.Drawing.Image. When using GetPictureFromIPicture to do this you may get GDI Exceptions. This issue is discussed on this post:
AxHost.GetPictureFromIPicture does not work correctly on x64 systems
Here is the workaround from that page. (Resolved the error in my tests)
>> >>>
Posted by Stefan Cuypers on 5/15/2009 at 4:34 AM
Here's code that does work correctly all the time (you'll need to set a reference to stdole):
using System.Drawing.Imaging;
...
public static Image GetPictureFromIPicture
(stdole.IPictureDisp picture)
{
if (picture == null)
return null;
IntPtr hpal = IntPtr.Zero;
if (picture.Type == 1) // bitmap
{
try
{
hpal = new IntPtr(picture.hPal);
}
catch (COMException)
{
}
}
Image retval = GetPictureFromParams
(new IntPtr(picture.Handle), picture.Type,
hpal, picture.Width, picture.Height);
GC.KeepAlive(picture); // Be sure we keep this picture alive
// until we copied it safely into our image.
return retval;
}
private static Image GetPictureFromParams
(IntPtr handle, int type, IntPtr paletteHandle,
int width, int height)
{
switch (type)
{
case -1:
return null;
case 0:
return null;
case 1:
return Image.FromHbitmap(handle,
paletteHandle);
case 2:
{
WmfPlaceableFileHeader wmfHeader =
new WmfPlaceableFileHeader();
wmfHeader.BboxRight = (short)width;
wmfHeader.BboxBottom = (short)height;
return (Image)new Metafile
(handle, wmfHeader, false).Clone();
}
case 3:
return (Image)Icon.FromHandle(handle).Clone();
case 4:
return (Image)new Metafile(handle, false).Clone();
}
throw new ArgumentException("AXUnknownImage", "type");
}
<< <<<
PictureBox – Thumbnail stretched
When using the Thumbnail of a drawing converted from iPictureDisp to Drawing.System.Image in a PictureBox, it may look stretched. A solution is to set the SizeMode property to StretchImage. This forces the image to be the size of the picture box. The picture box should be square and then the image will look correct.