usingSystem;usingSystem.Drawing;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Windows.Forms;usingSystem.Data;usingSystem.IO;namespacewindowsapplication3{/// <summary> ///a summary description of the Form1. /// </summary> Public classForm1:System.Windows.Forms.Form {PrivateSystem.Windows.Forms.Timer timer1; PrivateSystem.Windows.Forms.PictureBox PictureBox1; PrivateSystem.ComponentModel.IContainer components; PublicForm1 () {// //The Windows Forms Designer supports the required//InitializeComponent (); // //TODO: Add any constructor code after the InitializeComponent call// } /// <summary> ///clean up all the resources that are in use. /// </summary> protected Override voidDispose (BOOLdisposing) { if(disposing) {if(Components! =NULL) {components. Dispose (); } } Base. Dispose (disposing); } #regionWindows Form Designer generated code/// <summary> ///The Designer supports the required method-do not use the Code editor to modify///the contents of this method. /// </summary> Private voidInitializeComponent () { This. components =NewSystem.ComponentModel.Container (); This. timer1 =NewSystem.Windows.Forms.Timer ( This. components); This. PictureBox1 =NewSystem.Windows.Forms.PictureBox (); This. SuspendLayout (); // //PictureBox1// This. picturebox1.location =NewSystem.Drawing.Point ( -, -); This. Picturebox1.name ="PictureBox1"; This. picturebox1.size =NewSystem.Drawing.Size (416,272); This. Picturebox1.tabindex =0; This. Picturebox1.tabstop =false; // //Form1// This. AutoScaleBaseSize =NewSystem.Drawing.Size (6, -); This. ClientSize =NewSystem.Drawing.Size (472,310); This. Controls.AddRange (Newsystem.windows.forms.control[] { This. PictureBox1}); This. Name ="Form1"; This. Text ="Form1"; This. Load + =NewSystem.EventHandler ( This. Form1_Load); This. ResumeLayout (false); } #endregion /// <summary> ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {Application.Run (NewForm1 ()); } Private voidForm1_Load (Objectsender, System.EventArgs e) {VIEWDWG VIEWDWG=Newviewdwg (); pictureBox1.Image=viewdwg.getdwgimage ("C:\\\\1.DWG"); } } classVIEWDWG {structBitmapfileheader { Public ShortBftype; Public intbfsize; Public ShortbfReserved1; Public ShortBfReserved2; Public intbfoffbits; } PublicImage Getdwgimage (stringFileName) { if(!(File.exists (FileName))) { Throw NewFileNotFoundException ("The file was not found"); } FileStream DWGF; //file Stream intPossentinel;//location of the file description blockBinaryReader BR;//To read a binary file intTypepreview;//thumbnail format intPosbmp;//thumbnail position intLenbmp;//Thumbnail size ShortbiBitCount;//thumbnail bit depthBitmapfileheader BiH;//bmp file Header, DWG file does not contain the bitmap file header, to add it yourself byte[] bmpinfo;//bmp file body contained in a DWG fileMemoryStream BMPF =NewMemoryStream ();//save a bitmap's memory file streamBinaryWriter BMPR =NewBinaryWriter (BMPF);//Write a binary file classImage myimg =NULL; Try{DWGF=NewFileStream (FileName, FileMode.Open, FileAccess.Read);//file StreamBR =NewBinaryReader (DWGF); Dwgf.seek ( -, Seekorigin.begin);//read starting from 13th bytePossentinel = Br. ReadInt32 ();//the 13th to 17th byte indicates the position of the thumbnail description blockDwgf.seek (Possentinel + -, Seekorigin.begin);//move the pointer to the 31st byte of the thumbnail description blockTypepreview = Br. ReadByte ();//The 31st byte is the thumbnail format information, 2 is the BMP format, and 3 is the WMF format if(Typepreview = =1) { } Else if(Typepreview = =2|| Typepreview = =3) {posbmp= Br. ReadInt32 ();//where is the bitmap saved by the DWG fileLenbmp = Br. ReadInt32 ();//size of the bitmapDwgf.seek (Posbmp + -, Seekorigin.begin);//Move Pointer -to-place tilebiBitCount = Br. ReadInt16 ();//Read bit depthDwgf.seek (Posbmp, Seekorigin.begin);//reads all bitmap contents from the beginning of the bitmap blockBmpinfo = Br. Readbytes (lenbmp);//bitmap information that does not contain the header of a fileBr. Close (); Dwgf.close (); Bih.bftype=19778;//Creating a bitmap file header if(biBitCount <9) {bih.bfsize= Wu+4* (int) (Math.pow (2, biBitCount)) +lenbmp; } Else{bih.bfsize= Wu+lenbmp; } bih.bfreserved1=0;//Reserved bytesBih.bfreserved2 =0;//Reserved bytesBih.bfoffbits = -+ ++1024x768;//image Data Offset//The following begins writing to the bitmap file headerBmpr. Write (Bih.bftype);//File TypeBmpr. Write (bih.bfsize);//File SizeBmpr. Write (bih.bfreserved1);//0Bmpr. Write (BIH.BFRESERVED2);//0Bmpr. Write (bih.bfoffbits);//image Data OffsetBmpr. Write (Bmpinfo);//Write BitmapBMPF. Seek (0, Seekorigin.begin);//move the pointer to the beginning of the filemyimg = Image.fromstream (BMPF);//Create a bitmap file objectBMPR. Close (); BMPF. Close (); } returnmyimg; } Catch(Exception ex) {Throw NewException (ex. Message); } } }}
C # Implementation Preview DWG file full source code (no AutoCAD environment required)