如何在捕获的屏幕截图上绘图
How to draw on the screenshot captured
我已经使用 C# 代码捕获了电流 window,代码如下所示。我想在捕获的部分上绘制一个矩形(用于突出显示)。任何人都知道如何使用 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;
}
如果您想以编程方式绘制,也许可以尝试 System.Drawing。
您可能不需要第一行。
// Load the image (probably from your stream)
Image image = Image.FromFile( imagePath );
using (Graphics g = Graphics.FromImage(image))
{
// Modify the image using g here...
// Create a brush with an alpha value and use the g.FillRectangle function
SolidBrush shadowBrush = new SolidBrush(Red);
g.DrawRectangle(shadowBrush, /*RectanglePointsHere*/);
}
image.Save( imageNewPath );
我已经使用 AutomationElement 传递了单击点和单击的元素,并使用元素 BoundingRectangle 属性 绘制了一个日食。代码如下所示。
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# 代码捕获了电流 window,代码如下所示。我想在捕获的部分上绘制一个矩形(用于突出显示)。任何人都知道如何使用 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;
}
如果您想以编程方式绘制,也许可以尝试 System.Drawing。
您可能不需要第一行。
// Load the image (probably from your stream)
Image image = Image.FromFile( imagePath );
using (Graphics g = Graphics.FromImage(image))
{
// Modify the image using g here...
// Create a brush with an alpha value and use the g.FillRectangle function
SolidBrush shadowBrush = new SolidBrush(Red);
g.DrawRectangle(shadowBrush, /*RectanglePointsHere*/);
}
image.Save( imageNewPath );
我已经使用 AutomationElement 传递了单击点和单击的元素,并使用元素 BoundingRectangle 属性 绘制了一个日食。代码如下所示。
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;
}