随笔 - 3458, 文章 - 0, 评论 - 739, 阅读 - 1188万
  管理

C# 开发截图程序应用详细教程

Posted on   lzhdim  阅读(13)  评论(0编辑  收藏  举报

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

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

项目准备

创建 Windows Forms 应用程序

  1. 打开 Visual Studio
  2. 创建新项目 -> Windows Forms 应用程序
  3. 项目名称: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(1010),
                Size = new Size(12030)
            };
            btnFullScreen.Click += BtnFullScreen_Click;
            this.Controls.Add(btnFullScreen);

            // 区域截图按钮
            Button btnAreaCapture = new Button
            {
                Text = "区域截图",
                Location = new Point(1050),
                Size = new Size(12030)
            };
            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 和屏幕捕获的基本原理。

(评论功能已被禁用)
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2009-02-25 随笔 - 踏雪寻梅
2025年3月3日 星期一 【蛇】己卯月辛未日 乙巳年 二月初四 全国爱耳日
您的IP:3.15.193.22,操作系统:未知操作系统,浏览器:未知浏览器
Copyright (C) 2000-2025 Lzhdim Software All Rights Reserved
点击右上角即可分享
微信分享提示