C # generate CAD thumbnails or pictures

Source: Internet
Author: User

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 the file description block
BinaryReader Br=null; To read a binary file
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 the bitmap file header, to add it yourself
Byte[] Bmpinfo; BMP file body contained in a DWG file
MemoryStream BMPF = new MemoryStream (); Save a bitmap's memory file stream
BinaryWriter BMPR = new BinaryWriter (BMPF); Write a binary file class
System.Drawing.Image myimg = null;
Try
{

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

br = new BinaryReader (DWGF);
Dwgf.seek (Seekorigin.begin); Read starting 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 the thumbnail format information, 2 is the BMP format, and 3 is the WMF format
if (Typepreview = = 1)
{
}
else if (Typepreview = = 2 | | Typepreview = = 3)
{
Posbmp = Br. ReadInt32 (); Where is the bitmap saved by the DWG file

Dwgf.seek (Possentinel +, seekorigin.begin);//Originally this sentence is not, through and VC version of the single-step debugging compared to,

If this is not the case, the smaller DWG can be opened, but large (such as 1.5M or more) Open will be wrong, for unknown reasons


Lenbmp = Br. ReadInt32 (); Size of the bitmap
Dwgf.seek (Posbmp +, seekorigin.begin); Move pointer-to-place tile
biBitCount = Br. ReadInt16 (); Read bit depth
Dwgf.seek (Posbmp, Seekorigin.begin); Reads all bitmap contents from the beginning of the bitmap block
Bmpinfo = Br. Readbytes (lenbmp); Bitmap information that does not contain the header of a file
Br. Close ();
Dwgf.close ();
Bih.bftype = 19778; Creating a bitmap file header
if (biBitCount < 9)
{
Bih.bfsize = 4 * (int) (Math.pow (2, biBitCount)) + lenbmp;
}
Else
{
Bih.bfsize = si + 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); Move the pointer 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 = = "An attempt was made to move the file pointer before 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 ();

}
}

read out of the background color is white, the effect is poor, a lot of color display does not come out, at that time thought to show DWG file error, asked some master, (hehe, others told themselves to take out the white background, need to change the background color, despise yourself here) so continue to return the image object with C # operations , change the background color

///
Show DWG files
///
The width to display
The height to display
///
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; × < 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;
}

C # generate CAD thumbnails or pictures

Related Article

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.