これは、画面とマウス カーソルをスクリーンショットとしてキャプチャするために使用しているクラスです。しかし、フォームが画面の中央にある場合、フォーム自体ではなく、画面とフォームの背後の領域をキャプチャしないようにしたいと考えています。
フォームが前面にあり、アプリケーションの実行中にボタンをクリックしたり、フォーム内の何かを変更したりしても、フォームが存在しないようにフォームの背後の領域をキャプチャし続けます。
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;
}
}
つまり、実行中に Form が表示され、ボタンをクリックして変更できるようになりますが、キャプチャしたスクリーンショットを Paint で編集すると Form が表示されないということです。
これは Form1 にあり、キャプチャを作成する方法は次のとおりです。
private void StartRecording_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
そして timer1 tick イベント:
private void timer1_Tick(object sender, EventArgs e)
{
using (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true))
{
ffmp.PushFrame(bitmap);
}
}
この行は実際のキャプチャを行います: using (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true))