the problem is that no matter where and when i capture the screenshot it’s capturing it without the red point or a bit after the point has drawn and then the mouse cursor is no longer at the same position.
i want that exactly when i click the mouse down and it draw the point to capture the screenshot.
in form1 i have a picturebox and this is the code:
in this code it capture the mouse cursor at the right position but not capturing the red point.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mouse_Test
{
public partial class Form1 : Form
{
List<Point> points = new List<Point>();
private bool draw = false;
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
points.Add(e.Location);
draw = true;
pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (Point p in points)
{
e.Graphics.FillEllipse(Brushes.Red, new Rectangle(p.X, p.Y, 10, 10));
if (draw)
{
using (var bitmap = CaptureMouseCursor.Capture(0, 0, 500, 500))
{
bitmap.Save(@"d:screen.png");
}
draw = false;
}
}
}
}
}
the result is:
so, i tried to remove in the paint the whole bitmap.Save part before drawing the point but still it captures only the mouse cursor.
i want to capture both the moment the point is drawing and the mouse cursor when the mouse cursor is on the point when doing mousedown. but it always captures it without the point.
and this is the class, a bit long:
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Mouse_Test
{
public static class CaptureMouseCursor
{
public static class User32
{
public const Int32 CURSOR_SHOWING = 0x00000001;
[StructLayout(LayoutKind.Sequential)]
public struct ICONINFO
{
public bool fIcon;
public Int32 xHotspot;
public Int32 yHotspot;
public IntPtr hbmMask;
public IntPtr hbmColor;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public Int32 x;
public Int32 y;
}
[StructLayout(LayoutKind.Sequential)]
public struct CURSORINFO
{
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public POINT ptScreenPos;
}
[DllImport("user32.dll")]
public static extern bool GetCursorInfo(out CURSORINFO pci);
[DllImport("user32.dll")]
public static extern IntPtr CopyIcon(IntPtr hIcon);
[DllImport("user32.dll")]
public static extern bool DrawIcon(IntPtr hdc, int x, int y, IntPtr hIcon);
[DllImport("user32.dll")]
public static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo);
}
public static Bitmap Capture(int x, int y, int width, int height)
{
var bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(x, y, 0, 0, bitmap.Size, CopyPixelOperation.SourceCopy);
User32.CURSORINFO cursorInfo;
cursorInfo.cbSize = Marshal.SizeOf(typeof(User32.CURSORINFO));
if (User32.GetCursorInfo(out cursorInfo))
{
// if the cursor is showing draw it on the screen shot
if (cursorInfo.flags == User32.CURSOR_SHOWING)
{
// we need to get hotspot so we can draw the cursor in the correct position
var iconPointer = User32.CopyIcon(cursorInfo.hCursor);
User32.ICONINFO iconInfo;
int iconX, iconY;
if (User32.GetIconInfo(iconPointer, out iconInfo))
{
// calculate the correct position of the cursor
iconX = cursorInfo.ptScreenPos.x - ((int)iconInfo.xHotspot);
iconY = cursorInfo.ptScreenPos.y - ((int)iconInfo.yHotspot);
// draw the cursor icon on top of the captured screen image
User32.DrawIcon(g.GetHdc(), iconX, iconY, cursorInfo.hCursor);
// release the handle created by call to g.GetHdc()
g.ReleaseHdc();
}
}
}
}
return bitmap;
}
}
}