// C # Read the thumbnail of a CAD file (DWG file)
// 2010-09-04 16:34:58 | category: C # | font size subscription
// Read the thumbnail of the DWG file without using the task plug-in, so that you can browse on a computer where AutoCAD is not installed.
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. IO;
Browse DWG using namespace
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void form1_load (Object sender, eventargs E)
{
Viewdwg = new viewdwg ();
Picturebox1.image = viewdwg. getdwgimage ("C: \ 1.dwg ");
}
Class viewdwg
{
Struct bitmapfileheader
{
Public short bftype;
Public int bfsize;
Public short bfreserved1;
Public short bfreserved2;
Public int bfoffbits;
}
Public Image getdwgimage (string filename)
{
If (! (File. exists (filename )))
{
Throw new filenotfoundexception ("file not found ");
}
Filestream dwgf; // file stream
Int possentinel; // file description block location
Binaryreader BR; // read binary files
Int typepreview; // thumbnail format
Int posbmp; // The Position of the thumbnail.
Int lenbmp; // The thumbnail size.
Short bibitcount; // The bitdepth of the thumbnail.
Bitmapfileheader BiH; // BMP file header. the DWG file does not contain the bitmap file header.
Byte [] BMP Info; // The BMP file body contained in the DWG File
Memorystream bmp f = new memorystream (); // stores the bitmap memory file stream
Binarywriter BMP r = new binarywriter (bmp f); // write binary file class
Image myimg = NULL;
Try
{
Dwgf = new filestream (filename, filemode. Open, fileaccess. Read); // file stream
BR = new binaryreader (dwgf );
Dwgf. Seek (13, seekorigin. Begin); // read from 13th bytes
Possentinel = Br. readint32 (); // 13th to 17 bytes indicate the location of the thumbnail description block
Dwgf. Seek (possentinel + 30, seekorigin. Begin); // move the pointer to 31st bytes of the thumbnail description block
Typepreview = Br. readbyte (); // 31st bytes indicates the thumbnail format, 2 indicates the BMP format, and 3 indicates the WMF Format.
If (typepreview = 1)
{
}
Else if (typepreview = 2 | typepreview = 3)
{
Posbmp = Br. readint32 (); // location of the saved bitmap of the DWG File
Lenbmp = Br. readint32 (); // bitmap size
Dwgf. Seek (posbmp + 14, seekorigin. Begin); // move the pointer to the position graph block.
Bibitcount = Br. readint16 (); // read the bit depth
Dwgf. Seek (posbmp, seekorigin. Begin); // read all bitmap content from the beginning of the bitmap block.
BMP info = Br. readbytes (lenbmp); // bitmap information that does not contain the file header
BR. Close ();
Dwgf. Close ();
BiH. bftype = 19778; // create a bitmap file header
If (bibitcount <9)
{
BiH. bfsize = 54 + 4 * (INT) (math. Pow (2, bibitcount) + lenbmp;
}
Else
{
BiH. bfsize = 54 + lenbmp;
}
BiH. bfreserved1 = 0; // reserved bytes
BiH. bfreserved2 = 0; // reserved bytes
BiH. bfoffbits = 14 + 40 + 1024; // image data offset
// Write the following Bitmap File Header
Bmp r. Write (BIH. bftype); // file type
Bmp r. Write (BIH. bfsize); // File Size
Bmp r. Write (BIH. bfreserved1); // 0
Bmp r. Write (BIH. bfreserved2); // 0
Bmp r. Write (BIH. bfoffbits); // image data offset
Bmp r. Write (BMP info); // write a bitmap
Bmp f. Seek (0, seekorigin. Begin); // move the pointer to the beginning of the file
Myimg = image. fromstream (bmp f); // create a bitmap object
Bmp r. Close ();
Bmp f. Close ();
}
Return myimg;
}
Catch (exception ex)
{
Throw new exception (ex. Message );
}
}
}
}
}