C # preview the complete source code of a DWG file (without the need for an AutoCAD environment)

Source: Internet
Author: User
  1. Using system;
  2. Using system. drawing;
  3. Using system. collections;
  4. Using system. componentmodel;
  5. Using system. Windows. forms;
  6. Using system. Data;
  7. Using system. IO;
  8. Namespace windowsapplication3
  9. {
  10. /// <Summary>
  11. /// Summary of form1.
  12. /// </Summary>
  13. Public class form1: system. Windows. Forms. Form
  14. {
  15. Private system. Windows. Forms. Timer timer1;
  16. Private system. Windows. Forms. picturebox picturebox1;
  17. Private system. componentmodel. icontainer components;
  18. Public form1 ()
  19. {
  20. //
  21. // Required for Windows Form Designer support
  22. //
  23. Initializecomponent ();
  24. //
  25. // Todo: add Any constructor code after initializecomponent calls
  26. //
  27. }
  28. /// <Summary>
  29. /// Clear all resources in use.
  30. /// </Summary>
  31. Protected override void dispose (bool disposing)
  32. {
  33. If (disposing)
  34. {
  35. If (components! = NULL)
  36. {
  37. Components. Dispose ();
  38. }
  39. }
  40. Base. Dispose (disposing );
  41. }
  42. # Region windows Form Designer generated code
  43. /// <Summary>
  44. /// The designer supports the required methods-do not use the code editor to modify
  45. /// Content of this method.
  46. /// </Summary>
  47. Private void initializecomponent ()
  48. {
  49. This. components = new system. componentmodel. Container ();
  50. This. timer1 = new system. Windows. Forms. Timer (this. components );
  51. This. picturebox1 = new system. Windows. Forms. picturebox ();
  52. This. suspendlayout ();
  53. //
  54. // Picturebox1
  55. //
  56. This. picturebox1.location = new system. Drawing. Point (16, 16 );
  57. This. picturebox1.name = "picturebox1 ";
  58. This. picturebox1.size = new system. Drawing. Size (416,272 );
  59. This. picturebox1.tabindex = 0;
  60. This. picturebox1.tabstop = false;
  61. //
  62. // Form1
  63. //
  64. This. autoscalebasesize = new system. Drawing. Size (6, 14 );
  65. This. clientsize = new system. Drawing. Size (472,310 );
  66. This. Controls. addrange (new system. Windows. Forms. Control [] {
  67. This. picturebox1 });
  68. This. Name = "form1 ";
  69. This. Text = "form1 ";
  70. This. Load + = new system. eventhandler (this. form#load );
  71. This. resumelayout (false );
  72. }
  73. # Endregion
  74. /// <Summary>
  75. /// Main entry point of the application.
  76. /// </Summary>
  77. [Stathread]
  78. Static void main ()
  79. {
  80. Application. Run (New form1 ());
  81. }
  82. Private void form1_load (Object sender, system. eventargs E)
  83. {
  84. Viewdwg = new viewdwg ();
  85. Picturebox1.image = viewdwg. getdwgimage ("C :\\\ 1.dwg ");
  86. }
  87. }
  88. Class viewdwg
  89. {
  90. Struct bitmapfileheader
  91. {
  92. Public short bftype;
  93. Public int bfsize;
  94. Public short bfreserved1;
  95. Public short bfreserved2;
  96. Public int bfoffbits;
  97. }
  98. Public Image getdwgimage (string filename)
  99. {
  100. If (! (File. exists (filename )))
  101. {
  102. Throw new filenotfoundexception ("file not found ");
  103. }
  104. Filestream dwgf; // file stream
  105. Int possentinel; // file description block location
  106. Binaryreader BR; // read binary files
  107. Int typepreview; // thumbnail format
  108. Int posbmp; // The Position of the thumbnail.
  109. Int lenbmp; // The thumbnail size.
  110. Short bibitcount; // The bitdepth of the thumbnail.
  111. Bitmapfileheader BiH; // BMP file header. the DWG file does not contain the bitmap file header.
  112. Byte [] BMP Info; // The BMP file body contained in the DWG File
  113. Memorystream bmp f = new memorystream (); // stores the bitmap memory file stream
  114. Binarywriter BMP r = new binarywriter (bmp f); // write binary file class
  115. Image myimg = NULL;
  116. Try
  117. {
  118. Dwgf = new filestream (filename, filemode. Open, fileaccess. Read); // file stream
  119. BR = new binaryreader (dwgf );
  120. Dwgf. Seek (13, seekorigin. Begin); // read from 13th bytes
  121. Possentinel = Br. readint32 (); // 13th to 17 bytes indicate the location of the thumbnail description block
  122. Dwgf. Seek (possentinel + 30, seekorigin. Begin); // move the pointer to 31st bytes of the thumbnail description block
  123. Typepreview = Br. readbyte (); // 31st bytes indicates the thumbnail format, 2 indicates the BMP format, and 3 indicates the WMF Format.
  124. If (typepreview = 1)
  125. {
  126. }
  127. Else if (typepreview = 2 | typepreview = 3)
  128. {
  129. Posbmp = Br. readint32 (); // location of the saved bitmap of the DWG File
  130. Lenbmp = Br. readint32 (); // bitmap size
  131. Dwgf. Seek (posbmp + 14, seekorigin. Begin); // move the pointer to the position graph block.
  132. Bibitcount = Br. readint16 (); // read the bit depth
  133. Dwgf. Seek (posbmp, seekorigin. Begin); // read all bitmap content from the beginning of the bitmap block.
  134. BMP info = Br. readbytes (lenbmp); // bitmap information that does not contain the file header
  135. BR. Close ();
  136. Dwgf. Close ();
  137. BiH. bftype = 19778; // create a bitmap file header
  138. If (bibitcount <9)
  139. {
  140. BiH. bfsize = 54 + 4 * (INT) (math. Pow (2, bibitcount) + lenbmp;
  141. }
  142. Else
  143. {
  144. BiH. bfsize = 54 + lenbmp;
  145. }
  146. BiH. bfreserved1 = 0; // reserved bytes
  147. BiH. bfreserved2 = 0; // reserved bytes
  148. BiH. bfoffbits = 14 + 40 + 1024; // image data offset
  149. // Write the following Bitmap File Header
  150. Bmp r. Write (BIH. bftype); // file type
  151. Bmp r. Write (BIH. bfsize); // File Size
  152. Bmp r. Write (BIH. bfreserved1); // 0
  153. Bmp r. Write (BIH. bfreserved2); // 0
  154. Bmp r. Write (BIH. bfoffbits); // image data offset
  155. Bmp r. Write (BMP info); // write a bitmap
  156. Bmp f. Seek (0, seekorigin. Begin); // move the pointer to the beginning of the file
  157. Myimg = image. fromstream (bmp f); // create a bitmap object
  158. Bmp r. Close ();
  159. Bmp f. Close ();
  160. }
  161. Return myimg;
  162. }
  163. Catch (exception ex)
  164. {
  165. Throw new exception (ex. Message );
  166. }
  167. }
  168. }
  169. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.