Viewing Thumbnails

Update for Vault 2012:  Additional image formats supported. This post applies to Vault 2011 and earlier. Go here for the Vault 2012 code.

Here is how you view thumbnail data from Vault.

Background:
Thumbnails are stored as metafiles, which must be rendered to a display context.  This is a bit different from a jpeg because metafiles have no fixed size.  This is nice because the image can be scaled to any size without loss of detail.

Sample Code:
Once you have found the PropInst object with the thumbnail value, here is how you can render it to a System.Drawing.Image object.

C#:

 

/// <summary>
///
Takes a thumbnail property from Vault and renders it to an Image
/// </summary>
private System.Drawing.Image RenderThumbnailToImage(Document.PropInst propInst, int width, int height)
{
    // convert the property value to a byte array
    byte[] thumbnailRaw = (byte[])propInst.Val;

    // load the data into a stream but skip the first 12 bytes
    Stream stream = new MemoryStream(thumbnailRaw, 12, thumbnailRaw.Length – 12);

    // create the Metafile object
    System.Drawing.Imaging.Metafile metafile = new System.Drawing.Imaging.Metafile(stream);

    // render the Metafile to an Image object and display in the picture box
    Image retVal = metafile.GetThumbnailImage(width, height, new Image.GetThumbnailImageAbort(GetThumbnailImageAbort), IntPtr.Zero);
    stream.Close(); 
    return retVal;
}

/// <summary>
///
Delagate for the GetThumnailImage function.
/// </summary>
private static bool GetThumbnailImageAbort()
{
    return false;  // do nothing

}

VB.NET:

 

''' <summary>
''' Takes a thumbnail property from Vault and renders it to an Image
'''
</summary>
Private Function RenderThumbnailToImage(ByVal propInst As Document.PropInst, ByVal width As Integer, ByVal height As Integer) As System.Drawing.Image

    ' convert the property value to a byte array
    Dim thumbnailRaw As Byte() = CType(propInst.Val, Byte())

    ' load the data into a stream but skip the first 12 bytes
    Dim stream As System.IO.Stream = New System.IO.MemoryStream(thumbnailRaw, 12, thumbnailRaw.Length – 12)

    ' create the Metafile object
    Dim metafile As System.Drawing.Imaging.Metafile = New System.Drawing.Imaging.Metafile(stream)

    ' render the Metafile to an Image object and display in the picture box
    Dim retVal As Image = metafile.GetThumbnailImage(width, height, New Image.GetThumbnailImageAbort(AddressOf GetThumbnailImageAbort), IntPtr.Zero)

    stream.Close() 
    Return retVal
End Function

''' <summary>
''' Delagate for the GetThumnailImage function.
'''
</summary>
Private Shared Function GetThumbnailImageAbort() As Boolean 
    Return False  ' do nothing
End Function


Comments

3 responses to “Viewing Thumbnails”

  1. Your C# can be simplified slightly with a lambda; you also can use “using” rather than an explicit close. I also wonder whether the Metafile should be Dispose()d too:
    private System.Drawing.Image RenderThumbnailToImage(Document.PropInst propInst, int width, int height)
    {
    // convert the property value to a byte array
    var thumbnailRaw = (byte[])propInst.Val;
    // load the data into a stream but skip the first 12 bytes
    const int bytesToSkip = 12;
    using (var stream = new MemoryStream(thumbnailRaw, bytesToSkip, thumbnailRaw.Length – bytesToSkip))
    {
    // create the Metafile object
    using (var metafile = new System.Drawing.Imaging.Metafile(stream))
    {
    // render the Metafile to an Image object and display in the picture box
    return metafile.GetThumbnailImage(width, height, () => false, IntPtr.Zero);
    }
    }
    }
    I’m enjoying the articles; the File Associations – Part 1 was very nice.

  2. varinder Singh Avatar
    varinder Singh

    Is there a way to create thumbnail for non-CAD files, like image files.

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading