CAD二次开发根据多个实体获取边界截图

//加下多个实体,获取所有实体矩形边界,获取边界点截图
public void SnapShot(List<Entity> entities ,string name) { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; Database db = HostApplicationServices.WorkingDatabase; Extents3d extents3D = new Extents3d(); foreach (Entity ent in entities) { extents3D.AddExtents(ent.Bounds.Value); } Point3d P1 = new Point3d(extents3D.MinPoint.X - 2, extents3D.MinPoint.Y - 2, extents3D.MinPoint.Z); Point3d P2 = new Point3d(extents3D.MaxPoint.X + 2, extents3D.MaxPoint.Y + 2, extents3D.MaxPoint.Z); GetBitmapBy2Point(P1, P2, name); } public void GetBitmapBy2Point(Point3d p1, Point3d p2,string filename) { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; ViewTableRecord cvtr = ed.GetCurrentView(); ViewTableRecord vtr = new ViewTableRecord(); vtr.Width = Math.Abs(p1.X - p2.X); vtr.Height = Math.Abs(p2.Y - p1.Y); vtr.CenterPoint = new Point2d((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2); ed.SetCurrentView(vtr); ed.Regen(); var size = doc.Window.DeviceIndependentSize; Bitmap bmp = Autodesk.AutoCAD.ApplicationServices.DocumentExtension.CapturePreviewImage(doc, Convert.ToUInt32(size.Width), Convert.ToUInt32(size.Height)); double vW = vtr.Width; double vH = vtr.Height; int bW = bmp.Width; int bH = bmp.Height; string path = Path + string.Format("{0}.png", filename); ed.WriteMessage(path); if (vH / vW < bH / bW) { vH = bW / vW * vH; vW = bW; if (IsPixelFormatIndexed(bmp.PixelFormat)) { Bitmap bmp1 = new Bitmap(Convert.ToInt32(vW), Convert.ToInt32(vH), PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmp1)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(bmp, new Rectangle(0, 0, bmp1.Width, bmp1.Height), new Rectangle(0, Convert.ToInt32(bH / 2 - vH / 2), Convert.ToInt32(vW), Convert.ToInt32(vH)), GraphicsUnit.Pixel); g.Dispose(); //bmp1.Save("hh1.png", System.Drawing.Imaging.ImageFormat.Png); bmp.Save(path); } } } else { vW = bH / vH * vW; vH = bH; if (IsPixelFormatIndexed(bmp.PixelFormat)) { Bitmap bmp1 = new Bitmap(Convert.ToInt32(vW), Convert.ToInt32(vH), PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(bmp1)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.DrawImage(bmp, new Rectangle(0, 0, bmp1.Width, bmp1.Height), new Rectangle(Convert.ToInt32(bW / 2 - vW / 2), 0, Convert.ToInt32(vW), Convert.ToInt32(vH)), GraphicsUnit.Pixel); g.Dispose(); //bmp1.Save("hh1.png", System.Drawing.Imaging.ImageFormat.Png); bmp.Save(path); } } } }

 

posted @ 2022-03-07 09:09  百年一梦  阅读(518)  评论(0编辑  收藏  举报