明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: tcsl9621

[图形系统] 转发 完美解决VB.NET窗体中预览DWG图形(附完整代码)

  [复制链接]
发表于 2011-6-26 21:56 | 显示全部楼层
翻译成C#,同志们 看看是否有问题

private struct BITMAPFILEHEADER
{
public short bfType;
public int bfSize;
public short bfReserved1;
public short bfReserved2;
public int bfOffBits;
}
public Image GetDwgImage(string FileName)
{
Image functionReturnValue = default(Image);
if (!File.Exists(FileName))
  return functionReturnValue;
FileStream DwgF = default(FileStream);
//文件流
int PosSentinel = 0;
//文件描述块的位置
BinaryReader br = default(BinaryReader);
//读取二进制文件
int TypePreview = 0;
//缩略图格式
int PosBMP = 0;
//缩略图位置
int LenBMP = 0;
//缩略图大小
short biBitCount = 0;
//缩略图比特深度
BITMAPFILEHEADER biH = default(BITMAPFILEHEADER);
//BMP文件头,DWG文件中不包含位图文件头,要自行加上去
byte[] BMPInfo = null;
//包含在DWG文件中的BMP文件体
MemoryStream BMPF = new MemoryStream();
//保存位图的内存文件流
BinaryWriter bmpr = new BinaryWriter(BMPF);
//写二进制文件类
Image myImg = default(Image);
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格式
  switch (TypePreview) {
   case 1:
    break;
   case 2:
   case 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();
    var _with1 = biH;
    //建立位图文件头
    _with1.bfType = 0x4d42;
    if (biBitCount < 9)
     _with1.bfSize = 54 + 4 * (Math.Pow(2, biBitCount)) + LenBMP;
    else
     _with1.bfSize = 54 + LenBMP;
    _with1.bfReserved1 = 0;
    //保留字节
    _with1.bfReserved2 = 0;
    //保留字节
    _with1.bfOffBits = 14 + 0x28 + 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);
    //创建位图文件对象
    return myImg;
    bmpr.Close();
    BMPF.Close();
    break;
  }
} catch (Exception ex) {
  return null;
}
return functionReturnValue;

}
发表于 2011-6-26 23:26 | 显示全部楼层
收藏了,谢谢!!
发表于 2011-6-27 09:41 | 显示全部楼层
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);
   }
  }
}
}
发表于 2011-7-15 22:19 | 显示全部楼层
谢谢楼主分享
发表于 2011-7-16 16:27 | 显示全部楼层
在哪里下载?没有看见哦
发表于 2015-12-19 02:04 | 显示全部楼层
顶起来顶起来
发表于 2015-12-31 14:25 | 显示全部楼层
深入学习中……谢谢
发表于 2016-1-9 16:47 | 显示全部楼层
了不起   真的了不起啊   学习了
发表于 2016-1-9 22:47 | 显示全部楼层
不错 VB.NET 调用成功 能预览了
再研究研究
发表于 2016-3-11 11:59 | 显示全部楼层
虽然是老贴子了,很有学习价值。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-3-29 20:49 , Processed in 0.139264 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表