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

摘要:
=null){components.Dispose();}}base.Dispose;}#regionWindowsFormDesignergeneratedcode//////设计器支持所需的方法-不要使用代码编辑器修改///此方法的内容。///privatevoidInitializeComponent(){this.components=newSystem.ComponentModel.Container();this.timer1=newSystem.Windows.Forms.Timer;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;this.pictureBox1.TabIndex=0;this.pictureBox1.TabStop=false;////Form1//this.AutoScaleBaseSize=newSystem.Drawing.Size;this.ClientSize=newSystem.Drawing.Size;this.Controls.AddRange;this.Name="Form1";this.Text="Form1";this.Load+=newSystem.EventHandler;this.ResumeLayout;}#endregion//////应用程序的主入口点。
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);
   }
  }
 }
}zzhua

免责声明:文章转载自《C# 实现预览dwg文件完整源代码(无需autocad环境)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇2-7-搭建DNS服务器实现域名解析VirtualBox 及一些特殊USB(无驱动)的识别问题(二)解决了不能识别一些特殊USB的问题(其实就是插着USB重启,很简单)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

【Android】缩略图Thumbnails

在Android,多媒体文件(视频和图片)都是有缩略图的,在很多应用中,我们需要获取这些缩略图。比如最近在做一个类似相册的应用,需要扫描相册里面的图片,然后获取其缩略图,使用GridView去展示缩略图,当点击之后,我们需要获取其原始图,所以相关的需求如下: 1)获取缩略图(一个问题是:是否所有的图片以及视频都有缩略图?); 2)将缩略图和原始图关联起来;...

c语言数字图像处理(一):bmp图片格式及灰度图片转换

本篇文章首先介绍了bmp图片格式,主要参考wiki上的内容,包括bmp文件的存储方式,对于一些常见的bmp文件格式都给了例子,并且对8位 16位RGB555 16位RGB565格式的bmp文件进行了简单分析,最后的代码可以将8位,16位,24位,32位色彩深度的bmp文件转化位8位灰度图片,用作后续文章中算法的测试图片。 Bmp file structur...

Windows API参考大全(转)

具体用法请参考msdn 1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAddConnection3 创建同一个网络资源的连接 WNetCancelConnection 结束一个网络连接 WNetCancelConnection2 结束一个...

Delphi中使用调色板创建渐变色位图

 //创建调色板函数,R,G,B颜色分量,0-1之间取值  1 FUNCTION MaxGradientPalette(CONST RedFactor, GreenFactor, BlueFactor:  Single):  hPalette; 2   VAR 3     i             :  INTEGER; 4     Logic...

Flash务实主义(五)——AS3的垃圾回收

GC和内存泄露无关 垃圾回收,这次是一个被无数人讨论过的传统话题。 Action Script使用的是和Java相似的内存管理机制,并不会即时回收废弃对象的内存,而是在特定时间统一执行一次GC(Gabage Collection)操作来释放废弃对象的内存,避免了重复判断是否需要回收产生的性能问题。 但要注意,这只是决定回收的时机,而不是回收的内容。这个延迟...

windows xp查看缩略图时有缩略图没有文件名

windows xp查看缩略图时有缩略图没有文件名 当我们在资源管理器中以缩略图形式显示图形文件的时候,所有的图形文件都会以缩小的形式把自己的内容显示出来,不过同时还显示了文件名。如果你想在显示缩略图的时候隐藏文件名,那只要在查看菜单下切换到缩略图模式的同时按下Shift键就可以了,反过来操作就是可以恢复显示文件名...