编辑
2025-10-13
C#
00

目录

项目概述
项目准备
创建 Windows Forms 应用程序
核心代码实现
主窗体设计
FrmSelection.cs
注意事项
总结

项目概述

在本教程中,我们将使用 Windows Forms 开发一个功能完整的截图应用程序。这个应用程序将支持以下核心功能:

  • 全屏截图
  • 区域选择截图
  • 保存截图到指定位置
  • 复制截图到剪贴板

项目准备

创建 Windows Forms 应用程序

  1. 打开 Visual Studio
  2. 创建新项目 -> Windows Forms 应用程序
  3. 项目名称:ScreenshotApp

核心代码实现

主窗体设计

C#
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

C#
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); } } } }

image.png

image.png

注意事项

  • 需要引用 System.Drawing 命名空间
  • 确保有足够的系统权限进行屏幕捕获
  • 建议在 Windows 系统上运行

总结

通过本教程,您已经学会了如何使用 C# 开发一个功能完整的截图应用程序。希望这个示例能帮助您理解 Windows Forms 和屏幕捕获的基本原理。

本文作者:技术老小子

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!