C#实现截屏功能

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Bitmap bitmap = CaptureActiveScreen();
bitmap.Save("screenshot.png", ImageFormat.Png);
}

public Bitmap CaptureActiveScreen()
{
// 创建一个和当前屏幕一样大小的Bitmap
Rectangle bounds = Screen.GetBounds(Point.Empty);
Bitmap bmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);

// 创建一个画布
Graphics g = Graphics.FromImage(bmp);

// 截取整个屏幕
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);

// 释放画布资源
g.Dispose();

return bmp;
}
}
}

posted @ 2024-05-09 11:14  huanglg  阅读(305)  评论(0编辑  收藏  举报