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. /// 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. // Windows 窗体设计器支持所必需的
  22. //
  23. InitializeComponent();
  24. //
  25. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  26. //
  27. }
  28. /// <summary>
  29. /// 清理所有正在使用的资源。
  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. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  45. /// 此方法的内容。
  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.Form1_Load);
  71. this.ResumeLayout(false);
  72. }
  73. #endregion
  74. /// <summary>
  75. /// 应用程序的主入口点。
  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 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("文件没有被找到");
  103. }
  104. FileStream DwgF;  //文件流
  105. int PosSentinel;  //文件描述块的位置
  106. BinaryReader br;  //读取二进制文件
  107. int TypePreview;  //缩略图格式
  108. int PosBMP;       //缩略图位置
  109. int LenBMP;       //缩略图大小
  110. short biBitCount; //缩略图比特深度
  111. BITMAPFILEHEADER biH; //BMP文件头,DWG文件中不包含位图文件头,要自行加上去
  112. byte[] BMPInfo;       //包含在DWG文件中的BMP文件体
  113. MemoryStream BMPF = new MemoryStream(); //保存位图的内存文件流
  114. BinaryWriter bmpr = new BinaryWriter(BMPF); //写二进制文件类
  115. Image myImg = null;
  116. try
  117. {
  118. DwgF = new FileStream(FileName, FileMode.Open, FileAccess.Read);   //文件流
  119. br = new BinaryReader(DwgF);
  120. DwgF.Seek(13, SeekOrigin.Begin); //从第十三字节开始读取
  121. PosSentinel = br.ReadInt32();  //第13到17字节指示缩略图描述块的位置
  122. DwgF.Seek(PosSentinel + 30, SeekOrigin.Begin);  //将指针移到缩略图描述块的第31字节
  123. TypePreview = br.ReadByte();  //第31字节为缩略图格式信息,2 为BMP格式,3为WMF格式
  124. if (TypePreview == 1)
  125. {
  126. }
  127. else if (TypePreview == 2 || TypePreview == 3)
  128. {
  129. PosBMP = br.ReadInt32(); //DWG文件保存的位图所在位置
  130. LenBMP = br.ReadInt32(); //位图的大小
  131. DwgF.Seek(PosBMP + 14, SeekOrigin.Begin); //移动指针到位图块
  132. biBitCount = br.ReadInt16(); //读取比特深度
  133. DwgF.Seek(PosBMP, SeekOrigin.Begin); //从位图块开始处读取全部位图内容备用
  134. BMPInfo = br.ReadBytes(LenBMP); //不包含文件头的位图信息
  135. br.Close();
  136. DwgF.Close();
  137. biH.bfType = 19778; //建立位图文件头
  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; //保留字节
  147. biH.bfReserved2 = 0; //保留字节
  148. biH.bfOffBits = 14 + 40 + 1024; //图像数据偏移
  149. //以下开始写入位图文件头
  150. bmpr.Write(biH.bfType); //文件类型
  151. bmpr.Write(biH.bfSize);  //文件大小
  152. bmpr.Write(biH.bfReserved1); //0
  153. bmpr.Write(biH.bfReserved2); //0
  154. bmpr.Write(biH.bfOffBits); //图像数据偏移
  155. bmpr.Write(BMPInfo); //写入位图
  156. BMPF.Seek(0, SeekOrigin.Begin); //指针移到文件开始处
  157. myImg = Image.FromStream(BMPF); //创建位图文件对象
  158. bmpr.Close();
  159. BMPF.Close();
  160. }
  161. return myImg;
  162. }
  163. catch(Exception ex)
  164. {
  165. throw new Exception(ex.Message);
  166. }
  167. }
  168. }
  169. }

转自:http://www.it610.com/article/1761216.htm

C# 实现预览dwg文件完整源代码(无需autocad环境)相关推荐

  1. autocad型源代码_C# 实现预览dwg文件完整源代码(无需autocad环境)

    using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...

  2. winform界面嵌入dwg图纸_C# 实现预览dwg文件完整源代码(无需autocad环境)

    using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...

  3. winform界面嵌入dwg图纸_WPF中使用WinForm控件预览DWG文件(学习笔记)

    操作环境:XP,C# ,.Net4.0和VS2010中测试 WinForm中使用DWGThumbnail不用这么麻烦,下面讨论的是在WPF中使用,WPF中无法直接引用DWGThumbnail.ocx来 ...

  4. html预览dwg文件,大佬救命!有关dwg文件预览的问题

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 请问有人做过dwg的文件预览吗,网上的代码有版本限制,用最新版的AutoCAD保存的dwg文件会提示参数无效,有大佬做过这个吗? 异常信息: 未处理Sys ...

  5. html预览dwg文件,如何使用纯javascript autodesk在查看器中脱机显示二维(.dwg)文件

    请确保已完全下载所有提取的DWG可见气泡,并且要加载的模型路径正确,因为错误代码5代表 NETWORK_FILE_NOT_FOUND . var options = { env: 'Local', } ...

  6. CAD(dxf、dwg格式)文件的读取和显示,真正实现通过代码预览CAD文件,包含解析dwg、dxf文件,可以提取标注信息,可以转换为pdf、png、tiff、gif等6种格式的文件,可以永久免费实用

    真正实现通过代码预览CAD文件,包含解析dwg.dxf文件,可以提取标注信息,可以转换为pdf.png.tiff.gif等6种格式的文件,可以永久免费实用. 网上看了很多资料,不是缺这个就是少那个,反 ...

  7. winform界面嵌入dwg图纸_完美解决窗体中预览DWG图形(C#版)

    看到完美解决VB.NET窗体中预览DWG图形帖子后,用C#代码 实现如下: class ViewDWG { struct BITMAPFILEHEADER { public short bfType; ...

  8. android 在线预览pdf文件(目前最全)

    android原生webView不支持预览pdf文件,ios却可以,所以android想要实现在线预览webView要通过其他方法,有以下几种方法: 一.andorid原生自带的pdf管理库,主要提供 ...

  9. 文件预览:使用xlsx预览excel文件

    文件预览系列: mavon-editor预览Markdown文件 xlsx预览excel文件 注意事项: 多sheet页的情况需要自己手动处理 一.安装插件:xlsx // 我目前使用的是0.17.5 ...

最新文章

  1. 在Python中检查类型的规范方法是什么?
  2. IL语言之.ctor
  3. html5 建筑物模型,基于HTML5的建筑物阴影实时模拟
  4. php odbc 分页,用php实现odbc数据分页显示一例_php技巧
  5. MySql默认编码所造成的乱码麻烦1.222
  6. windows_study_2
  7. vlookup 2张表 显示na_【Excel 函数】Vlookup 正反向查询
  8. 初识大数据(四. 大数据与人工智能的关系)
  9. 程序员的遗憾:为什么我没早学数据分析?
  10. ci源码解析之CodeIgniter.php
  11. JavaScript 习题及面试题 3
  12. oracle存储过程导出scv文件
  13. C#如何消除按键提示声音?
  14. C++中的DLL调用0x00000000错误
  15. 解决微信小程序wx:if使用不了函数,WXS使用方法以及防踩坑
  16. 奥克兰硕士计算机专业学费,【2018新西兰奥克兰大学硕士研究生各专业学费一览】 新西兰奥克兰大学学费...
  17. 卓训教育:孩子不爱说话,性格内向怎么办?
  18. 835616-60-9,4-Fluoro-thalidomide用于补充CRBN蛋白的沙利度胺基脑啡肽配体
  19. 路边停车系统的具体流程是什么
  20. 使用DOS重定位技术执行isqlw(SQL查询分析器)

热门文章

  1. 文件服务器锁定账户,文件服务器账户权限设置
  2. qq四国军旗2.1 beat03 builde018记牌器开发思路(三)
  3. 微信小程序轮播图渲染(示例)
  4. 人民日报探店联想安定门店,联想智慧零售变革现成效
  5. DOS下格式化移动硬盘
  6. 蓝桥杯-Sine之舞-java
  7. 怎样在VI编辑器中使用鼠标移动光标(鼠标点哪里,光标移动到哪里)
  8. 《Android 应用 之路》 每日一文简单版Kotlin
  9. 职场上罕见张小敬,办公室常常有元载
  10. (十三)有一点心动 - 6