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;
        }
    }


}

 

posted on   HRDK  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义



点击右上角即可分享
微信分享提示