Viewing Thumbnails

Updated for Vault 2012: New image types are now supported for thumbnails.  In Vault 2011 and earlier, Metafile was the only image type.  In Vault 2012, anything that can be streamed into System.Drawing.Image is supported. 

This post contains the updated code.  The old code can be found here.


Basically there are two possible algorithms, and you don't know which one to use upfront.  So the code tries one algorithm.  If it fails, the second algorithm is run.

private System.Drawing.Image GetImage(PropInst prop, int width, int height)
{
    byte[] thumbnailRaw = (byte[])prop.Val;
    System.Drawing.Image retVal = null;
    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(thumbnailRaw))
    {
        // we don't know what format the image is in, so we try a couple of formats
        try
        {
            // try the meta file format
            ms.Seek(12, System.IO.SeekOrigin.Begin);
            System.Drawing.Imaging.Metafile metafile = 
                new System.Drawing.Imaging.Metafile(ms);
            retVal = metafile.GetThumbnailImage(width, height, 
                new System.Drawing.Image.GetThumbnailImageAbort(GetThumbnailImageAbort), 
                System.IntPtr.Zero);
        }
        catch 
        {
            // I guess it's not a met
afile
            retVal = null;
        }
 

        if (retVal == null)
        {
            try
            {
                // try to stream to System.Drawing.Image
                ms.Seek(0, System.IO.SeekOrigin.Begin);
                System.Drawing.Image rawImage = System.Drawing.Image.FromStream(ms, true);
                retVal = rawImage.GetThumbnailImage(width, height, 
                    new System.Drawing.Image.GetThumbnailImageAbort(GetThumbnailImageAbort), 
                    System.IntPtr.Zero);
            }
            catch
            {
                // not compatible Image object
                retVal = null;
            }
        }
    }
    return retVal;
}

private static bool GetThumbnailImageAbort()
{
    return false;
}

Private Function GetImage(ByVal prop As PropInst, ByVal width As Integer, _
                ByVal height As Integer)
_
                As System.Drawing.Image
    Dim thumbnailRaw As Byte() = DirectCast(prop.Val, Byte())
    Dim retVal As System.Drawing.Image = Nothing
    Using ms As New System.IO.MemoryStream(thumbnailRaw)

       
' we don't know what format the image is in, so we try a couple of formats
        Try
            ' try the meta file format
            ms.Seek(12, System.IO.SeekOrigin.Begin)
            Dim metafile As New System.Drawing.Imaging.Metafile(ms)
            retVal = metafile.GetThumbnailImage(width, height, _
              New System.Drawing.Image.GetThumbnailImageAbort( _
              AddressOf GetThumbnailImageAbort), _
              System.IntPtr.Zero)
        Catch
    &#016
0;      
' I guess it's not a metafile
            retVal = Nothing
        End Try

        If retVal Is Nothing Then
            Try
                ' try a serialized System.Drawing.Image
                ms.Seek(0, System.IO.SeekOrigin.Begin)
                Dim rawImage As System.Drawing.Image = System.Drawing.Image.FromStream(ms, True)
                retVal = rawImage.GetThumbnailImage(width, height, _
                  New System.Drawing.Image.GetThumbnailImageAbort( _
                 
AddressOf GetThumbnailImageAbort), _
                  System.IntPtr.Zero)
            Catch
                ' not a serialized Image object
                retVal = Nothing
            End Try
        End If
    End Using
    Return retVal
End
Function

Private Shared Function GetThumbnailImageAbort() As Boolean
    Return False
End Function


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading