gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:58:48 27 4
gpt4 key购买 nike

我使用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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com