如何在截图中绘制

时间:2018-02-20 05:52:38

标签: 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;
        }

2 个答案:

答案 0 :(得分:3)

我已经使用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;
        }

答案 1 :(得分:2)

如果您想以编程方式绘制,可以尝试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 );