public static Bitmap ExtractImage(this BlockTableRecord btr, int width = 500, int height = 500,int backColor = -1){Color color;if (backColor == -1){var preferences = (AcadPreferences)Autodesk.AutoCAD.ApplicationServices.Application.Preferences;int colorValue = Convert.ToInt32(preferences.Display.GraphicsWinModelBackgrndColor);int blue = (colorValue >> 16) & 0xFF;int green = (colorValue >> 8) & 0xFF;int red = colorValue & 0xFF;color = Color.FromArgb(255, red, green, blue);}else{color = Color.FromArgb(backColor);}var cl = Autodesk.AutoCAD.Colors.Color.FromColor(color);var imgPtr = Autodesk.AutoCAD.Internal.Utils.GetBlockImage(btr.Id, width, height, cl);if (imgPtr == IntPtr.Zero) return null;var image = Image.FromHbitmap(imgPtr);return image;}
https://m.jb51.net/article/157005.htm
c#生成cad缩略图或者图片
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("文件没有被找到");}FileStream DwgF=null; //文件流int PosSentinel; //文件描述块的位置BinaryReader br=null; //读取二进制文件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); //写二进制文件类System.Drawing.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文件保存的位图所在位置DwgF.Seek(PosSentinel + 16, SeekOrigin.Begin);//原来这一句是没有的,通过和VC版的单步调试相比较得来,//如果没有这一句,比较小的dwg能够打开,但是大的(比如1.5M以上)打开就会出错,原因不详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); //0bmpr.Write(biH.bfReserved2); //0bmpr.Write(biH.bfOffBits); //图像数据偏移bmpr.Write(BMPInfo); //写入位图BMPF.Seek(0, SeekOrigin.Begin); //指针移到文件开始处myImg = System.Drawing.Image.FromStream(BMPF); //创建位图文件对象bmpr.Close();BMPF.Close();}return myImg;}catch (EndOfStreamException){throw new EndOfStreamException("文件不是标准的DWG格式文件,无法预览!");}catch (IOException ex){if (ex.Message == "试图将文件指针移到文件开头之前。\r\n"){throw new IOException("文件不是标准的DWG格式文件,无法预览!");}else if (ex.Message == "文件“" + FileName + "”正由另一进程使用,因此该进程无法访问该文件。"){//复制文件,继续预览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();}}
读取出来的背景色为白色,效果比较差,很多颜色显示不出来,当时认为显示DWG文件出错误了,问了些高手,(呵呵,别人告诉自己本身取出的就是白色背景,需要自己改变背景色,在此鄙视一下自己)所以继续用C#操作返回的IMAGE对象,改变背景色//////显示DWG文件////// 要显示的宽度/// 要显示的高度///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;}
以上。