Recently contacted the CAD two times development, records the development process. Read a lot of information. (blue17300589)

Source: Internet
Author: User
Tags reserved

Recently contacted the CAD two times development, records the development process. Read a lot of information.

C # read DWG files

struct Bitmapfileheader
{
public short Bftype;
public int bfsize;
public short bfReserved1;
public short bfReserved2;
public int bfoffbits;
}
public static System.Drawing.Image Getdwgimage (String FileName)
{
if (!) ( File.exists (FileName))
{
throw new FileNotFoundException ("File not Found");
}

FileStream Dwgf=null; File stream
int Possentinel; Location of File description block
BinaryReader Br=null; Reading binary files
int Typepreview; Thumbnail format
int posbmp; Thumbnail position
int lenbmp; Thumbnail size
Short biBitCount; Thumbnail bit Depth
Bitmapfileheader BiH; BMP file Header, DWG file does not contain bitmap file header, to add up
Byte[] Bmpinfo; BMP file body included in DWG file
MemoryStream Bmpf = new MemoryStream (); Save a bitmap stream of memory files
BinaryWriter bmpr = new BinaryWriter (BMPF); Write binary file classes
System.Drawing.Image myimg = null;
Try
{

DWGF = new FileStream (FileName, FileMode.Open, FileAccess.Read); File stream

br = new BinaryReader (DWGF);
Dwgf.seek (Seekorigin.begin); Start reading from 13th byte
Possentinel = Br.   ReadInt32 (); The 13th to 17th byte indicates the position of the thumbnail description block
Dwgf.seek (Possentinel + seekorigin.begin); Move the pointer to the 31st byte of the thumbnail description block
Typepreview = Br.   ReadByte (); The 31st byte is thumbnail format information, 2 is BMP format, and 3 is WMF format
if (Typepreview = 1)
{
}
else if (Typepreview = 2 | | Typepreview = 3)
{
Posbmp = Br. ReadInt32 (); The location of the bitmap saved by the DWG file
Lenbmp = Br. ReadInt32 (); Size of bitmap
Dwgf.seek (Posbmp +, seekorigin.begin); Move the pointer in place of the tile
biBitCount = Br. ReadInt16 (); Read bit depth
Dwgf.seek (Posbmp, Seekorigin.begin); Read all bitmap content from the beginning of a bitmap block standby
Bmpinfo = Br. Readbytes (lenbmp); Bitmap information that does not contain the header of the file
Br. Close ();
Dwgf.close ();
Bih.bftype = 19778; To create a bitmap file header
if (biBitCount < 9)
{
Bih.bfsize = 4 * (int) (Math.pow (2, biBitCount)) + lenbmp;
}
Else
{
Bih.bfsize = + lenbmp;
}
bih.bfreserved1 = 0; Reserved bytes
Bih.bfreserved2 = 0; Reserved bytes
Bih.bfoffbits = 14 + 40 + 1024; Image data offset
The following begins writing to the bitmap file header
Bmpr. Write (Bih.bftype); File type
Bmpr.   Write (bih.bfsize); File size
Bmpr. Write (bih.bfreserved1); 0
Bmpr. Write (BIH.BFRESERVED2); 0
Bmpr. Write (bih.bfoffbits); Image data offset
Bmpr. Write (Bmpinfo); Write bitmap
Bmpf. Seek (0, seekorigin.begin); The pointer moves to the beginning of the file
myimg = System.Drawing.Image.FromStream (Bmpf); Create a bitmap file object
Bmpr. Close ();
Bmpf. Close ();
}
return myimg;
}
catch (EndOfStreamException)
{
throw new EndOfStreamException ("file is not a standard DWG format file and cannot be previewed.") ");
}
catch (IOException ex)
{
if (ex. Message = = "Attempts to move the file pointer to the beginning of the file. /r/n ")
{
throw new IOException ("file is not a standard DWG format file and cannot be previewed.") ");
}
else if (ex. Message = = "File" + filename + "" is being used by another process, so the process cannot access the file. ")
{
Copy files, continue previewing
File.Copy (FileName, Application.startuppath + @ "/LINSHI.DWG", true);
Image image = Getdwgimage (Application.startuppath + @ "/LINSHI.DWG");
File.delete (Application.startuppath + @ "/LINSHI.DWG");
return image;
}
Else
{
throw new Exception (ex. message);
}
}
catch (Exception ex)
{
throw new Exception (ex. message);
}
Finally
{
if (DWGF!= null)
{
Dwgf.close ();
}
if (br!= null)
{
Br. Close ();
}
Bmpr. Close ();
Bmpf. Close ();

}
}

The background color read is white, the effect is poor, a lot of color display does not come out, at that time to show DWG file out of error, asked some experts, (hehe, others told themselves to take out is a white background, need to change their background color, in this despise himself) so continue to use the C # operation returned Image object , change the background color

<summary>
Show DWG files
</summary>
<param name= "Pwidth" > Width to display </param>
<param name= "Pheight" > Height to display </param>
<returns></returns>
public static System.Drawing.Image showdwg (int pwidth,int pheight,string FilePath)
{
System.Drawing.Image Image = Getdwgimage (FilePath);
Bitmap Bitmap = new Bitmap (image);
int Height = bitmap. Height;
int Width = bitmap. Width;
Bitmap Newbitmap = new Bitmap (Width, Height);
Bitmap Oldbitmap = (Bitmap) Bitmap;
Color Pixel;
for (int x = 1; x < Width + x + +)
{
for (int y = 1; y < Height; y++)
{

Pixel = Oldbitmap. GetPixel (x, y);
int r = pixel. R, G = pixel. G, B = pixel. B
if (pixel. Name = = "FFFFFFFF" | | Pixel. Name = = "ff000000")
{
R = 255-pixel. R
g = 255-pixel. G
b = 255-pixel. B
}

Newbitmap. SetPixel (x, Y, Color.FromArgb (R, G, b));
}
}
Bitmap BT = new Bitmap (Newbitmap, Pwidth, pheight);

return newbitmap;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.