C# 实现预览dwg文件完整源代码(无需autocad环境)
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.IO;
- namespace WindowsApplication3
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.Timer timer1;
- private System.Windows.Forms.PictureBox pictureBox1;
- private System.ComponentModel.IContainer components;
- public Form1()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- this.timer1 = new System.Windows.Forms.Timer(this.components);
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
- this.SuspendLayout();
- //
- // pictureBox1
- //
- this.pictureBox1.Location = new System.Drawing.Point(16, 16);
- this.pictureBox1.Name = "pictureBox1";
- this.pictureBox1.Size = new System.Drawing.Size(416, 272);
- this.pictureBox1.TabIndex = 0;
- this.pictureBox1.TabStop = false;
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(472, 310);
- this.Controls.AddRange(new System.Windows.Forms.Control[] {
- this.pictureBox1});
- this.Name = "Form1";
- this.Text = "Form1";
- this.Load += new System.EventHandler(this.Form1_Load);
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- private void Form1_Load(object sender, System.EventArgs e)
- {
- ViewDWG 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("文件没有被找到");
- }
- FileStream DwgF; //文件流
- int PosSentinel; //文件描述块的位置
- BinaryReader br; //读取二进制文件
- int TypePreview; //缩略图格式
- int PosBMP; //缩略图位置
- int LenBMP; //缩略图大小
- short biBitCount; //缩略图比特深度
- BITMAPFILEHEADER biH; //BMP文件头,DWG文件中不包含位图文件头,要自行加上去
- byte[] BMPInfo; //包含在DWG文件中的BMP文件体
- MemoryStream BMPF = new MemoryStream(); //保存位图的内存文件流
- BinaryWriter bmpr = new BinaryWriter(BMPF); //写二进制文件类
- Image myImg = null;
- try
- {
- DwgF = new FileStream(FileName, FileMode.Open, FileAccess.Read); //文件流
- br = new BinaryReader(DwgF);
- DwgF.Seek(13, SeekOrigin.Begin); //从第十三字节开始读取
- PosSentinel = br.ReadInt32(); //第13到17字节指示缩略图描述块的位置
- DwgF.Seek(PosSentinel + 30, SeekOrigin.Begin); //将指针移到缩略图描述块的第31字节
- TypePreview = br.ReadByte(); //第31字节为缩略图格式信息,2 为BMP格式,3为WMF格式
- if (TypePreview == 1)
- {
- }
- else if (TypePreview == 2 || TypePreview == 3)
- {
- PosBMP = br.ReadInt32(); //DWG文件保存的位图所在位置
- LenBMP = br.ReadInt32(); //位图的大小
- DwgF.Seek(PosBMP + 14, SeekOrigin.Begin); //移动指针到位图块
- biBitCount = br.ReadInt16(); //读取比特深度
- DwgF.Seek(PosBMP, SeekOrigin.Begin); //从位图块开始处读取全部位图内容备用
- BMPInfo = br.ReadBytes(LenBMP); //不包含文件头的位图信息
- br.Close();
- DwgF.Close();
- biH.bfType = 19778; //建立位图文件头
- if (biBitCount < 9)
- {
- biH.bfSize = 54 + 4 * (int)(Math.Pow(2, biBitCount)) + LenBMP;
- }
- else
- {
- biH.bfSize = 54 + LenBMP;
- }
- biH.bfReserved1 = 0; //保留字节
- biH.bfReserved2 = 0; //保留字节
- biH.bfOffBits = 14 + 40 + 1024; //图像数据偏移
- //以下开始写入位图文件头
- bmpr.Write(biH.bfType); //文件类型
- bmpr.Write(biH.bfSize); //文件大小
- bmpr.Write(biH.bfReserved1); //0
- bmpr.Write(biH.bfReserved2); //0
- bmpr.Write(biH.bfOffBits); //图像数据偏移
- bmpr.Write(BMPInfo); //写入位图
- BMPF.Seek(0, SeekOrigin.Begin); //指针移到文件开始处
- myImg = Image.FromStream(BMPF); //创建位图文件对象
- bmpr.Close();
- BMPF.Close();
- }
- return myImg;
- }
- catch(Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- }
- }
C# 实现预览dwg文件完整源代码(无需autocad环境)的更多相关文章
- html页面预览pdf文件使用插件pdfh5.js
html预览pdf文件需要依赖pdf.js 移动端适配需要pdfh5.js 记录移动端适配pdfh5.js的用发 在线预览: https://www.gjtool.cn/pdfh5/pdf.html? ...
- linux在线预览pdf文件开发思路
准备:swftools,flexpaper 基本思路: 1,将pdf文件转化成swf文件 2,使用flexpaper预览swf文件 主要代码: 1,在linux中安装swftools.官网下载swft ...
- es5预览本地文件、es6练习代码演示案例
es6简单基础: <!DOCTYPE html> <html> <head> <meta name="viewport" content= ...
- 用pdf.js实现在移动端在线预览pdf文件
用pdf.js实现在移动端在线预览pdf文件1.下载pdf.js 官网地址:https://mozilla.github.io/pdf.js/ 2.配置 下载下来的文件包,就是一个demo ...
- WinForm中预览Office文件
WinForm预览Office文档 使用WinForm, WPF, Office组件 原理:使用Office COM组件将Word,Excel转换为XPS文档, 将WPF的DocumentViewer ...
- Android开发中遇到的问题(三)——eclipse创建android项目无法正常预览布局文件
一.问题描述 今天使用SDK Manager将Android SDK的版本更新到了Android 5.1的版本,eclipse创建android项目时,预览activity_main.xml文件时提示 ...
- 使用qmlscene预览qml文件
功能:可以预览qml文件的界面 使用:qmlscene myapp.qml
- 文档控件NTKO OFFICE 详细使用说明之预览Excel文件(查看、编辑、保存回服务器)
1.在线预览Excel文件 (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将www.ntko.com加入到浏 ...
- 文档控件NTKO OFFICE 详细使用说明之预览PDF文件(禁止打印、下载、另存为、防抓包下载)
1.在线预览PDF文件(禁止打印.下载.复制.另存为) (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将ww ...
随机推荐
- javascript 生成UUID
代码一: /*! Math.uuid.js (v1.4) http://www.broofa.com mailto:robert@broofa.com Copyright (c) 2010 Rober ...
- Oracle异常的抛出处理
--一异常处理的代码 --sqlcode 异常编号 --sqlerrm 信号字符串 /* 在plsql 块中格式 Declare 变量 Begin 代码块 EXCEPTION when 异常的名称 t ...
- linux虚拟机centos64位_6.5+VM10安装oracle11g图文详解
注意: vi基本命令:i--编辑状态 退出编辑并保存时先按ESC键,再按符合“:wq”或者":x"即可注意每个步骤时的当前用户,是root还是oracle 以root用户登录虚机 ...
- Ztree中simpleData是怎么转换成标准ztree数组对象的
今天遇到一个自己构造树的情况,树是动态的,预先不知道根节点,用的是easyUI中的tree,于是参考了下Ztree中的实现,恍然大悟,遂记之: transformTozTreeFormat: func ...
- Memory Analyzer Blog
引用:http://memoryanalyzer.blogspot.jp/2008/05/automated-heap-dump-analysis-finding.html Dienstag, 27. ...
- Xcode快捷键 ---- 提高效率
Mac中主要有四个修饰键,分别是Command,Control,Option和Shift. 1. ⌘ + L 搜索行数,输入行数,调到指定行数 2.⌘ + shift + O 查询flie ...
- C#获取客户端相关信息
1.获取Uri参数 2.获取客户端操作系统.浏览器信息 3.获取客户端分辨率 4.C#判断用户是手机访问还是PC访问
- 【6】Laravel5.1的migration数据库迁移
查看Laravel5.1的目录 当你配置好数据库后,在命令行执行下边的操作 php artisan migrate 打开数据库会发现,我们的数据库多了四个表,神奇吧! 打开任意一个migration查 ...
- [模拟炉石](一)让游戏过程显示到cocos2d中
在上篇中,如果运行了fireplace的tests/full_game.py,这个程序将一个游戏过程在终端上运行完成,可以看到整个过程,那么第一步要做的就是将这个过程显示到cocos2d创建的场景中去 ...
- ReactiveCocoa v2.5 源码解析 之 架构总览
ReactiveCocoa 是一个 iOS 中的函数式响应式编程框架,它受 Functional Reactive Programming 的启发,是 Justin Spahr-Summers 和 J ...