When i'm taking screenshots of my screen how can i capture the screen without capturing the Form win

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
When im running my application the Form is in the middle of the screen.
In this class i take screenshots capture the screen each 40ms.
I want that when im running my application i will be able to see the Form and will be able to click buttons change settings whatever i want to do.
But the class will not capture the Form . I dont want to hide the Form i want to see it when the application is running i just dont want the Form to be capture with the rest of the screen.
I want it to capture the area behind the Form.
So if i will open the screenshot in Paint later i will see the entire screen without the Form.
But when the application is running i will see the Form.
This is how i capture the screen :using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ScreenShotDemo
{
public class ScreenCapture
{
[StructLayout(LayoutKind.Sequential)]
struct CURSORINFO
{
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public POINTAPI ptScreenPos;
}

[StructLayout(LayoutKind.Sequential)]
struct POINTAPI
{
public int x;
public int y;
}

[DllImport("user32.dll")]
static extern bool GetCursorInfo(out CURSORINFO pci);

[DllImport("user32.dll")]
static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon);

const Int32 CURSOR_SHOWING = 0x00000001;

public static Bitmap CaptureScreen(bool CaptureMouse)
{
Bitmap result = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

try
{
using (Graphics g = Graphics.FromImage(result))
{
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

if (CaptureMouse)
{
CURSORINFO pci;
pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO));

if (GetCursorInfo(out pci))
{
if (pci.flags == CURSOR_SHOWING)
{
DrawIcon(g.GetHdc(), pci.ptScreenPos.x, pci.ptScreenPos.y, pci.hCursor);
g.ReleaseHdc();
}
}
}
}
}
catch
{
result = null;
}

return result;
}
}

And in Form1: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;
using ScreenShotDemo;
using System.Drawing.Imaging;
using System.IO;
using System.Diagnostics;
using DannyGeneral;
using System.Drawing.Drawing2D;

namespace ScreenVideoRecorder
{
public partial class Form1 : Form
{
Magnifier20070401.MagnifierForm mf;
ScreenCapture sc;
Bitmap bitmap;
Ffmpeg ffmp;

public Form1()
{
InitializeComponent();

ffmp = new Ffmpeg();
sc = new ScreenCapture();

Then i click the start recording button:private void StartRecording_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}

Then in the timer1 tick event:private void timer1_Tick(object sender, EventArgs e)
{
using (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true))
{
ffmp.PushFrame(bitmap);
}
}

This is the line that take the screenshots :using (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true))
So when running the application i want to see the Form and use the buttons and everything when i look on the screenshot i dont want to see the Form like it wasnt there at all.

View the full article
 
Back
Top