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. }

C# 实现预览dwg文件完整源代码(无需autocad环境)的更多相关文章

  1. html页面预览pdf文件使用插件pdfh5.js

    html预览pdf文件需要依赖pdf.js 移动端适配需要pdfh5.js 记录移动端适配pdfh5.js的用发 在线预览: https://www.gjtool.cn/pdfh5/pdf.html? ...

  2. linux在线预览pdf文件开发思路

    准备:swftools,flexpaper 基本思路: 1,将pdf文件转化成swf文件 2,使用flexpaper预览swf文件 主要代码: 1,在linux中安装swftools.官网下载swft ...

  3. es5预览本地文件、es6练习代码演示案例

    es6简单基础: <!DOCTYPE html> <html> <head> <meta name="viewport" content= ...

  4. 用pdf.js实现在移动端在线预览pdf文件

    用pdf.js实现在移动端在线预览pdf文件1.下载pdf.js    官网地址:https://mozilla.github.io/pdf.js/ 2.配置    下载下来的文件包,就是一个demo ...

  5. WinForm中预览Office文件

    WinForm预览Office文档 使用WinForm, WPF, Office组件 原理:使用Office COM组件将Word,Excel转换为XPS文档, 将WPF的DocumentViewer ...

  6. Android开发中遇到的问题(三)——eclipse创建android项目无法正常预览布局文件

    一.问题描述 今天使用SDK Manager将Android SDK的版本更新到了Android 5.1的版本,eclipse创建android项目时,预览activity_main.xml文件时提示 ...

  7. 使用qmlscene预览qml文件

    功能:可以预览qml文件的界面 使用:qmlscene myapp.qml

  8. 文档控件NTKO OFFICE 详细使用说明之预览Excel文件(查看、编辑、保存回服务器)

    1.在线预览Excel文件 (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将www.ntko.com加入到浏 ...

  9. 文档控件NTKO OFFICE 详细使用说明之预览PDF文件(禁止打印、下载、另存为、防抓包下载)

    1.在线预览PDF文件(禁止打印.下载.复制.另存为) (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将ww ...

随机推荐

  1. Android开发_关于中英文切换

    开发APP过程中可能要有中文模式和英文模式,切换后控件要随着进行更改,以下代码可以很好的实现: public static int getResourcesId(Context context, St ...

  2. 洛谷 P1066 2^k进制数

    P1066 2^k进制数 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. ( ...

  3. 给定范围内产生N个不同的随机数

    void RandNumbs(int nLimts, int result[], int n)//给定范围内产生n个不同随机数(1-nLimts),并存储到result中 { int nNum = 0 ...

  4. HDU 5044 离线LCA算法

    昨天写了HDU 3966 ,本来这道题是很好解得,结果我想用离线LCA 耍一把,结果发现离线LCA 没理解透,错了好多遍,终得AC ,这题比起 HDU 3966要简单,因为他不用动态查询.但是我还是错 ...

  5. Oracle遇到的一些问题

    1.制造数据时出现错误 ORA-30009问题 计划任务: insert into test select rownum,sysdate from dual connect 会出现ORA-30009 ...

  6. Git / Bower Errors: Exit Code # 128 & Failed connect

    今天第一次使用bower来安装插件,上来就报了这个错. 然后在google上查找,很多人都有做出回答,让执行如下 git config --global url.https://github.com/ ...

  7. Largest Rectangle in a Histogram(HDU 1506 动态规划)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. CloudStack API编程指引

    原文地址:https://cwiki.apache.org/confluence/display/CLOUDSTACK/CloudStack+API+Coding+Guidelines 前言 本文阐述 ...

  9. ArcGIS API for Silverlight开发入门

    你用上3G手机了吗?你可能会说,我就是喜欢用nokia1100,ABCDEFG跟我 都没关系.但你不能否认3G是一种趋势,最终我们每个人都会被包裹在3G网络中.1100也不是一成不变,没准哪天为了打击 ...

  10. svn: “sqlite: attempt to write a readonly database”

    原因很可能是在svn与本地同步的时候上锁了,可能没注意在svn执行与仓库同步的时候被中断,所以锁文件没有解锁,但是这样的错误,应该不是标题上所说的错误啊??搞不懂了,以前这样的错误,cleanup都有 ...