C# 两种方法截取活动窗口屏幕,实现窗体截图

方法1,截屏内容仅包括活动窗口界面,而方法2是从屏幕范围取图,截屏内容会包括屏幕上所有内容。例如有一些程序在桌面顶层显示半透明的悬浮窗,用方法2截屏就会包括这些内容,并不是单纯的活动窗口内容。
方法1,对一些有渐变效果的边框,截图会包括边框范围。方法2则可以把截屏范围限制在程序窗口,截屏范围更准确。

方法1:
1. 获得活动窗口的句柄
2. 根据句柄获得窗口坐标和大小.
3. 指定复制范围,从屏幕复制图像

IntPtr handle = Win32Api.GetForegroundWindow();
Win32Api.RECT rect;
int result = Win32Api.DwmGetWindowAttribute(hwnd, Win32Api.DWMWA_EXTENDED_FRAME_BOUNDS, out rect, Marshal.SizeOf(typeof(Win32Api.RECT)));

int width = rect.Width;
int height = rect.Height;
Bitmap bmp = new Bitmap(width, height);

using (Graphics g = Graphics.FromImage(bmp))
{
    g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size);
}

bmp.Save(screenshotPath, ImageFormat.Png);

方法2:
1. 获得活动窗口的句柄
2. 根据句柄获得 device context (DC) 
3. 从DC复制 bit-block

Win32Api.RECT rect;
Win32Api.GetWindowRect(hwnd, out rect);

Bitmap bmp = new Bitmap(rect.Width, rect.Height);
Graphics g = Graphics.FromImage(bmp);

IntPtr hdcDest = g.GetHdc();
IntPtr hdcSrc = Win32Api.GetWindowDC(hwnd);
Win32Api.BitBlt(hdcDest, 0, 0, rect.Width, rect.Height, hdcSrc, 0, 0, Win32Api.SRCCOPY);
g.ReleaseHdc(hdcDest);
Win32Api.ReleaseDC(hwnd, hdcSrc);
g.Dispose();
bmp.Save(screenshotPath, ImageFormat.Png);

win32 api调用网上都有,这里就不再重复了。

另外,截全屏幕,代码如下 :

public static void SaveFullScreenshot(string path)
        {
            Rectangle bounds = Screen.GetBounds(DrawingPoint.Empty);

            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(DrawingPoint.Empty, DrawingPoint.Empty, bounds.Size);
                }

                bitmap.Save(path, ImageFormat.Png);
            }
        }

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 C# WinForm 窗口实现屏幕录制,可以使用 .NET Framework 中提供的 `System.Drawing` 命名空间下的 `Screen` 和 `Graphics` 类,以及 `System.Windows.Forms` 命名空间下的 `Timer` 控件。 具体步骤如下: 1. 在窗口中添加 `PictureBox` 控件,用于显示录制的屏幕内容。 2. 添加一个 `Timer` 控件,设置其 `Interval` 属性为需要录制的帧率(例如 30 毫秒)。 3. 在 `Timer.Tick` 事件中,使用 `Graphics.CopyFromScreen` 方法屏幕内容拷贝到 `PictureBox` 控件中。 4. 启动 `Timer` 控件,开始录制屏幕。 以下是一个简单的示例代码: ```csharp public partial class Form1 : Form { private Timer timer; private Bitmap bitmap; public Form1() { InitializeComponent(); timer = new Timer(); timer.Interval = 30; // 30毫秒一帧 timer.Tick += Timer_Tick; } private void Timer_Tick(object sender, EventArgs e) { if (bitmap == null) { bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); } using (var g = Graphics.FromImage(bitmap)) { g.CopyFromScreen(0, 0, 0, 0, bitmap.Size); pictureBox1.Image = bitmap; } } private void button1_Click(object sender, EventArgs e) { timer.Start(); // 开始录制 } private void button2_Click(object sender, EventArgs e) { timer.Stop(); // 停止录制 } } ``` 在这个示例中,通过每隔 30 毫秒执行一次 `Timer_Tick` 事件,并在其中将屏幕内容拷贝到 `PictureBox` 控件中,实现屏幕录制的功能。通过点击 `button1` 和 `button2` 按钮来开始和停止录制。注意,由于屏幕录制可能会消耗大量的系统资源,建议在录制时关闭其他不必要的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值