Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:sdksamples:extractthumbnail [2015/09/14] (current)
Line 1: Line 1:
 +====== Extract Thumbnail Preview Image ======
 +====== C++, .NET ======
 +> **Summary:​** //​Demonstrates how to extract the thumbnail preview image from a 3dm file using the Rhino SDK.//
 +> **NOTICE:** //The Rhino.NET SDK is deprecated in Rhino 5.  This example adapted for the new RhinoCommon SDK is [[developer:​rhinocommonsamples:​extractthumbnail|here]]//​
 +=====Question=====
 +I would like to be able to display a 3dm file's thumbnail preview image in a dialog box. Is there an SDK function that will help me to do this?
 +
 +=====Answer=====
 +When Rhino reads a 3dm file, it ignores the thumbnail preview image stored in the file (since it is never used). Thus, if you want to obtain the thumbnail preview image for the current document, or any 3dm file, you will have to read the 3dm file yourself. Fortunatly, you only need to read a very small portion of the 3dm file to get the thumbnail preview image.
 +
 +Rhino stores a document'​s thumbnail preview image as an **ON_WindowsBitmap**,​ which is just an uncompressed Windows device independent bitmap, or DIB. At the heart of **ON_WindowsBitmap** is simply a Windows **BITMAPINFO** structure.
 +
 +The following sample code demonstrates how to read the thumbnail preview image from a 3dm file.
 +
 +===== C++ =====
 +<code c++>
 +
 +bool Read3dmThumbnailPreviewImage( const wchar_t* filename, ON_WindowsBitmap&​ bitmap )
 +{
 +  if( 0 == filename | 0 == filename[0] )
 +    return false;
 +
 +  try
 +  {
 +    FILE* archive_fp = ON::​OpenFile( filename, L"​rb"​ );
 +    if( 0 == archive_fp ) 
 +      return false;
 +
 +    ON_BinaryFile archive( ON::​read3dm,​ archive_fp );
 +
 +    // STEP 1: REQUIRED - Read start section
 +    int file_version = 0;
 +    ON_String strComments;​
 +    if( !archive.Read3dmStartSection(&​file_version,​ strComments) )
 +    {
 +      ON::​CloseFile( archive_fp );
 +      return false;
 +    }
 +
 +    // STEP 2: REQUIRED - Read properties section
 +    ON_3dmProperties properties;
 +    if( !archive.Read3dmProperties(properties) )
 +    {
 +      ON::​CloseFile( archive_fp );
 +      return false;
 +    }
 +
 +    ON::​CloseFile( archive_fp );
 +
 +    if( !properties.m_PreviewImage.IsValid() )
 +      return false;
 +
 +    bitmap = properties.m_PreviewImage;​
 +  }
 +
 +  catch(...) // Handle all exceptions
 +  {
 +    return false;
 +  }
 +
 +  return true;
 +}
 +
 +</​code>​
 +===== C# =====
 +<code c#>
 +
 +public bool Read3dmThumbnailPreviewImage(string filename, ref OnWindowsBitmap bitmap)
 +{
 +  if (0 == filename.Length)
 +    return false;
 +
 +  try
 +  {
 +    OnFileHandle archive_fp = OnUtil.OpenFile(filename,​ "​rb"​);​
 +    if (archive_fp == null)
 +      return false;
 +
 +    OnBinaryFile archive = new OnBinaryFile(IOn.archive_mode.read3dm,​ archive_fp);​
 +
 +    // STEP 1: REQUIRED - Read start section
 +    int file_version = 0;
 +    string strComments = ;
 +    if (!archive.Read3dmStartSection(ref file_version,​ ref strComments))
 +    {
 +      OnUtil.CloseFile(archive_fp);​
 +      return false;
 +    }
 +
 +    // STEP 2: REQUIRED - Read properties section
 +    On3dmProperties properties = new On3dmProperties();​
 +    if (!archive.Read3dmProperties(ref properties))
 +    {
 +      OnUtil.CloseFile(archive_fp);​
 +      return false;
 +    }
 +
 +    OnUtil.CloseFile(archive_fp);​
 +
 +    if (!properties.m_PreviewImage.IsValid())
 +      return false;
 +
 +    bitmap = properties.m_PreviewImage;​
 +  }
 +
 +  catch
 +  {
 +    return false;
 +  }
 +
 +  return true;
 +}
 +</​code>​
 +
 +
 +{{tag>​Developer cplusplus dotnet}}
  
developer/sdksamples/extractthumbnail.txt ยท Last modified: 2015/09/14 (external edit)