在本教程中,我们将使用 Windows Forms 开发一个功能完整的截图应用程序。这个应用程序将支持以下核心功能:
- 全屏截图
- 区域选择截图
- 保存截图到指定位置
- 复制截图到剪贴板
项目准备
创建 Windows Forms 应用程序
- 打开 Visual Studio
- 创建新项目 -> Windows Forms 应用程序
- 项目名称:ScreenshotApp
核心代码实现
主窗体设计
using System.Drawing.Imaging;
namespace AppScreenshot
{
public partial class FrmMain : Form
{
// 截图区域选择窗体
private FrmSelection selectionForm;
public FrmMain()
{
InitializeComponent();
InitializeButtons();
}
// 初始化按钮事件
private void InitializeButtons()
{
// 全屏截图按钮
Button btnFullScreen = new Button
{
Text = "全屏截图",
Location = new Point(10, 10),
Size = new Size(120, 30)
};
btnFullScreen.Click += BtnFullScreen_Click;
this.Controls.Add(btnFullScreen);
// 区域截图按钮
Button btnAreaCapture = new Button
{
Text = "区域截图",
Location = new Point(10, 50),
Size = new Size(120, 30)
};
btnAreaCapture.Click += BtnAreaCapture_Click;
this.Controls.Add(btnAreaCapture);
}
// 全屏截图方法
private void BtnFullScreen_Click(object sender, EventArgs e)
{
// 获取主屏幕
Rectangle bounds = Screen.PrimaryScreen.Bounds;
// 创建位图
using (Bitmap screenshot = new Bitmap(bounds.Width, bounds.Height))
{
// 使用 Graphics 捕获屏幕
using (Graphics graphics = Graphics.FromImage(screenshot))
{
graphics.CopyFromScreen(
bounds.X,
bounds.Y,
0,
0,
bounds.Size,
CopyPixelOperation.SourceCopy
);
}
// 保存截图
SaveScreenshot(screenshot);
}
}
// 区域截图方法
private void BtnAreaCapture_Click(object sender, EventArgs e)
{
// 显示区域选择窗体
selectionForm = new FrmSelection();
selectionForm.ScreenshotTaken += OnScreenshotTaken;
selectionForm.ShowDialog();
}
// 保存截图方法
private void SaveScreenshot(Image screenshot)
{
// 创建保存文件对话框
using (SaveFileDialog saveDialog = new SaveFileDialog())
{
saveDialog.Filter = "PNG 图像|*.png|JPEG 图像|*.jpg";
saveDialog.Title = "保存截图";
saveDialog.FileName = $"Screenshot_{DateTime.Now:yyyyMMdd_HHmmss}";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
// 保存图像
screenshot.Save(saveDialog.FileName,
saveDialog.FilterIndex == 1 ?
ImageFormat.Png :
ImageFormat.Jpeg
);
// 复制到剪贴板
Clipboard.SetImage(screenshot);
MessageBox.Show("截图已保存并复制到剪贴板", "成功",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
}
}
}
// 区域截图事件处理
private void OnScreenshotTaken(object sender, Image screenshot)
{
SaveScreenshot(screenshot);
}
}
}
FrmSelection.cs
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;
namespace AppScreenshot
{
public partial class FrmSelection : Form
{
private Point startPoint;
private Rectangle selectedRegion;
public event EventHandler<Image> ScreenshotTaken;
public FrmSelection()
{
InitializeComponent();
// 设置窗体为全屏透明
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
BackColor = Color.Black;
Opacity = 0.3;
ShowInTaskbar = false;
// 注册鼠标事件
MouseDown += SelectionForm_MouseDown;
MouseMove += SelectionForm_MouseMove;
MouseUp += SelectionForm_MouseUp;
}
private void SelectionForm_MouseDown(object sender, MouseEventArgs e)
{
// 记录起始点
startPoint = e.Location;
}
private void SelectionForm_MouseMove(object sender, MouseEventArgs e)
{
// 绘制选择区域
if (e.Button == MouseButtons.Left)
{
selectedRegion = new Rectangle(
Math.Min(startPoint.X, e.X),
Math.Min(startPoint.Y, e.Y),
Math.Abs(startPoint.X - e.X),
Math.Abs(startPoint.Y - e.Y)
);
Invalidate();
}
}
private void SelectionForm_MouseUp(object sender, MouseEventArgs e)
{
// 捕获选定区域
if (selectedRegion.Width > 0 && selectedRegion.Height > 0)
{
using (Bitmap screenshot = new Bitmap(selectedRegion.Width, selectedRegion.Height))
{
using (Graphics graphics = Graphics.FromImage(screenshot))
{
graphics.CopyFromScreen(
selectedRegion.X,
selectedRegion.Y,
0,
0,
selectedRegion.Size
);
}
// 触发截图事件
ScreenshotTaken?.Invoke(this, screenshot);
}
Close();
}
}
protected override void OnPaint(PaintEventArgs e)
{
// 绘制选择区域边框
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, selectedRegion);
}
}
}
}
注意事项
- 需要引用
System.Drawing
命名空间 - 确保有足够的系统权限进行屏幕捕获
- 建议在 Windows 系统上运行
总结
通过本教程,您已经学会了如何使用 C# 开发一个功能完整的截图应用程序。希望这个示例能帮助您理解 Windows Forms 和屏幕捕获的基本原理。
![]() |
Austin Liu 刘恒辉
Project Manager and Software Designer E-Mail:lzhdim@163.com Blog:https://lzhdim.cnblogs.com 欢迎收藏和转载此博客中的博文,但是请注明出处,给笔者一个与大家交流的空间。谢谢大家。 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2009-02-25 随笔 - 踏雪寻梅