AForge.Video录屏技术问题:录制画面中鼠标光标不可见
Hey there! Let’s fix that missing mouse cursor issue in your AForge.Video screen recordings— I know how frustrating it is to have all that hard work on active session recording almost done, only to hit this snag.
The core problem here is that AForge’s built-in ScreenCaptureProvider doesn’t capture mouse input by default. We need to manually grab the cursor’s position and image, then overlay it onto every frame before saving. Here’s how to implement this step by step:
1. Capture Mouse Position and Cursor Data
First, we need to get the current mouse position (relative to your recording area) and the active cursor image. Use Cursor.Position for screen coordinates, and Cursor.Current to access the cursor itself.
2. Overlay the Cursor on Each Video Frame
Hook into the NewFrame event of your ScreenCaptureProvider to modify each frame before it’s saved. Here’s a working code example:
private void ScreenCapture_NewFrame(object sender, NewFrameEventArgs eventArgs) { // Make a copy of the original frame to avoid modifying the source directly Bitmap frameWithCursor = (Bitmap)eventArgs.Frame.Clone(); // Get mouse position, adjusted to your recording area (e.g., active window bounds) Point mouseScreenPos = Cursor.Position; // If recording a specific window, subtract the window's top-left coordinates here mouseScreenPos.Offset(-_activeSessionBounds.X, -_activeSessionBounds.Y); // Draw the cursor onto the frame using (Graphics g = Graphics.FromImage(frameWithCursor)) { Cursor.Current.Draw(g, new Rectangle( mouseScreenPos.X, mouseScreenPos.Y, Cursor.Current.Size.Width, Cursor.Current.Size.Height )); } // Replace the original frame with the cursor-overlaid version eventArgs.Frame.Dispose(); eventArgs.Frame = frameWithCursor; }
3. Handle Edge Cases for Active Sessions
Since you’re recording only active sessions, add checks to ensure cursor capture works when the session switches:
- Track if the target session is active, and skip cursor drawing if it’s not.
- Update
_activeSessionBoundswhenever the active window changes to keep the mouse position accurate.
4. Fix Custom Cursor Issues (If Needed)
Some apps use custom cursors that Cursor.Current might not capture perfectly. If you run into this, use Win32 interop to grab the cursor directly:
[DllImport("user32.dll")] private static extern IntPtr GetCursor(); [DllImport("user32.dll")] private static extern bool GetIconInfo(IntPtr hIcon, out IconInfo pIconInfo); [DllImport("gdi32.dll")] private static extern bool DeleteObject(IntPtr hObject); private struct IconInfo { public bool fIcon; public int xHotspot; public int yHotspot; public IntPtr hbmMask; public IntPtr hbmColor; } private Bitmap GetCurrentCursorBitmap() { IntPtr cursorHandle = GetCursor(); GetIconInfo(cursorHandle, out IconInfo iconInfo); Bitmap cursorBitmap = Bitmap.FromHbitmap(iconInfo.hbmColor); DeleteObject(iconInfo.hbmColor); DeleteObject(iconInfo.hbmMask); return cursorBitmap; }
Then replace Cursor.Current.Draw() with code to draw this bitmap at the mouse position.
Quick Troubleshooting Tips
- Double-check that your mouse position adjustment matches your recording area (full screen vs. specific window). A wrong offset will make the cursor appear off-screen.
- Ensure you’re cloning the frame before modifying it— modifying the original
eventArgs.Framedirectly can cause unexpected behavior. - Test with different apps to verify both default and custom cursors are captured correctly.
I’ve used this exact approach to add cursor support to AForge-based screen recordings before, so it should work for your setup. Don’t worry— your active session recording feature will be fully functional soon!
内容的提问来源于stack exchange,提问作者Khurram Shaikh

