using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShotScreen
{
public class Program
{
private static Point p;
[CommandMethod("ssss")]
public void Start()
{
Application.PreTranslateMessage += Application_PreTranslateMessage;
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
var ppr1 = ed.GetPoint("请选择第一个点");
Point p1 = new Point(), p2 = new Point();
if (ppr1.Status != PromptStatus.OK)
{
return;
}
else if (ppr1.Status == PromptStatus.OK)
{
p1 = new Point(p.X,p.Y);
}
Point3d bp = ppr1.Value;
var ppr2 = ed.GetCorner("请选择第二个点", bp);
if (ppr2.Status != PromptStatus.OK)
{
return;
}
else if (ppr2.Status == PromptStatus.OK)
{
p2 = new Point(p.X, p.Y);
}
try
{
Bitmap bitmap = Util.SavePng(p1, p2);
bitmap.Save("D:\\test.png", System.Drawing.Imaging.ImageFormat.Png);
}
catch (System.Exception)
{
}
finally
{
Application.PreTranslateMessage -= Application_PreTranslateMessage;
}
}
private void Application_PreTranslateMessage(object sender, PreTranslateMessageEventArgs e)
{
p.X = e.Message.pt_x;
p.Y = e.Message.pt_y;
}
}
}
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Acap = Autodesk.AutoCAD.ApplicationServices.Application;
namespace ShotScreen
{
public class Util
{
[DllImport("user32.dll")]
public static extern bool ClientToScreen(IntPtr hWnd, ref System.Drawing.Point pt);
public static Bitmap SavePng(Point p1, Point p2)
{
Rectangle tScreenRect = new Rectangle(
Math.Min(p1.X, p2.X),
Math.Min(p1.Y, p2.Y),
Math.Abs(p1.X - p2.X),
Math.Abs(p1.Y - p2.Y)
);
Bitmap tSrcBmp = new Bitmap(tScreenRect.Width, tScreenRect.Height);
using (Graphics gp = Graphics.FromImage(tSrcBmp))
{
gp.CopyFromScreen(Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y), 0, 0, tScreenRect.Size);
//gp.DrawImage(tSrcBmp, 0, 0, tScreenRect, GraphicsUnit.Pixel);
}
return tSrcBmp;
}
public static Point GetScreenPointFromCadPoint(Point3d point)
{
var doc = Acap.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var wcsPoint = point.TransformBy(ed.CurrentUserCoordinateSystem);
var hWnd = doc.Window.Handle;
var vp = (short)Acap.GetSystemVariable("CVPORT");
System.Windows.Point wdPt = ed.PointToScreen(wcsPoint, vp);
var scale = Graphics.FromHwnd(IntPtr.Zero).DpiX / 96.0f;
var drawingPt = new System.Drawing.Point(Convert.ToInt32(wdPt.X * scale), Convert.ToInt32(wdPt.Y * scale));
ClientToScreen(hWnd, ref drawingPt);
return drawingPt;
}
}
}
标签:p2,截图,p1,Point,System,var,二次开发,using,CAD From: https://www.cnblogs.com/HRDK-CADeveloper/p/18642557