c# - 如何在捕获的屏幕截图上绘图

标签 c# screenshot screen-capture

我使用C#代码捕获了当前窗口,代码如下所示。我想在捕获的部分上绘制一个矩形(用于突出显示内容)。有人知道如何使用 C# 代码突出显示内容吗?我想在下面提到的光标位置上绘图。

 public Bitmap CaptureScreen()
        {
            enmScreenCaptureMode screenCaptureMode = enmScreenCaptureMode.Window;
            Rectangle bounds;

            if (screenCaptureMode == enmScreenCaptureMode.Screen)
            {
                bounds = Screen.GetBounds(Point.Empty);
                CursorPosition = Cursor.Position;
            }
            else
            {
                var foregroundWindowsHandle = GetForegroundWindow();
                var rect = new Rect();
                GetWindowRect(foregroundWindowsHandle, ref rect);
                bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
                CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);

            }

            var result = new Bitmap(bounds.Width, bounds.Height);


            using (var g = Graphics.FromImage(result))
            {
                Pen pen = new Pen(Color.Red);
                g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);

            }

            return result;
        }

最佳答案

我已经使用 AutomationElement 传递了单击的点和单击的元素,并使用元素 BoundingRectangle 属性绘制了一个 eclipse 。代码如下所示。

 public Bitmap CaptureScreen(System.Windows.Point point, AutomationElement element)
        {
            enmScreenCaptureMode screenCaptureMode = enmScreenCaptureMode.Window;
            Rectangle bounds;

            var foregroundWindowsHandle = GetForegroundWindow();
            var rect = new Rect();
            GetWindowRect(foregroundWindowsHandle, ref rect);
            if (screenCaptureMode == enmScreenCaptureMode.Screen)
            {
                bounds = Screen.GetBounds(Point.Empty);
                CursorPosition = Cursor.Position;
            }
            else
            {

                bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
                CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);
            }

            var result = new Bitmap(bounds.Width, bounds.Height);
            using (var g = Graphics.FromImage(result))
            {

                 Pen pen = new Pen(Color.Red,2);
                g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                //float drawX = (float)point.X - rect.Left;
                //float drawY = (float)point.Y - rect.Top;
                float drawX = (float)element.Current.BoundingRectangle.X - rect.Left;
                float drawY = (float)element.Current.BoundingRectangle.Y - rect.Top;
                g.DrawEllipse(pen, drawX, drawY, (float) element.Current.BoundingRectangle.Width, (float)element.Current.BoundingRectangle.Height);           
            }
            return result;
        }

关于c# - 如何在捕获的屏幕截图上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48878704/

相关文章:

c# - Entity Framework 6 将自定义 SQL 查询映射到实体

c# - 标准电子邮件类?

bitmap - CameraX 预览查看截图

java - 将 ViewGroup 捕获到图像文件中

java - 如何在 ARCore 中捕获每一帧

C# Socket 打印有时没有反应?

c# - 在 TFS 构建后通过远程桌面连接自动执行 IISRESET

ios - 检测在我的 iOS 应用中的任何应用上截取的屏幕截图

.net - 有没有免费的方法可以将 html 页面转换为带有 .net 的图像

java - 我应该(可以吗?)我重写 Robot 类以通过重复的屏幕捕获节省内存,或者 GC 是否足够好?