- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.IO;
- namespace WindowsApplication3
- {
-
-
-
- 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()
- {
-
-
-
- InitializeComponent();
-
-
-
- }
-
-
-
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows Form Designer generated code
-
-
-
-
- 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();
-
-
-
- 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;
-
-
-
- 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
-
-
-
- [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;
- byte[] BMPInfo;
- 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();
- DwgF.Seek(PosSentinel + 30, SeekOrigin.Begin);
- TypePreview = br.ReadByte();
- if (TypePreview == 1)
- {
- }
- else if (TypePreview == 2 || TypePreview == 3)
- {
- PosBMP = br.ReadInt32();
- 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);
- bmpr.Write(biH.bfReserved2);
- 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);
- }
- }
- }
- }
|
请发表评论