C# 练习1 全屏截图程序

C# 练习1 全屏截图程序

笔者在做项目(基于倍福EtherCAT控制系统开发操作界面)调试阶段,为了便于记录过程参数,利用C#写了一个截图小程序,故记录。

MainWindow.cs:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace Screenshot
{
    public partial class MainWindow : Form
    {
        public string _filePath ;
        public string _fileName;
        public string _suffix;                                //后缀
        public ImageFormat _imageFormat;
        // public string _cutSize;

	//鼠标截屏
        public bool _screenShotFlag;
        public bool _mouseDownFlag;
        public Point _start, _end;
	
	 public MainWindow()
        {
            InitializeComponent();
        }
        
	private void MainWindow_Load(object sender, EventArgs e)
        {
            //初始化
            _filePath = @"C:\Users\Lenovo\Desktop\C#\Screenshot\Image";    //默认保存路径
            _imageFormat = ImageFormat.Jpeg;                               //默认图片格式(JPG)
            _suffix = ".jpg";
            tbFilePath.Text = _filePath;

	    string time = DateTime.Now.Year.ToString() + 
                        string.Format("{0:D2}", DateTime.Now.Month)+ 
                        string.Format("{0:D2}", DateTime.Now.Day)+
                        string.Format("{0:D2}", DateTime.Now.Hour)+
                        string.Format("{0:D2}", DateTime.Now.Minute)+
                        string.Format("{0:D2}", DateTime.Now.Second);
            tnImageName.Text = time + _suffix;
            cbImageFormat.Text = ".JPG";
            cbHideMainWindow.Checked = true;                                //默认隐藏主窗口
            cbCutSize.Text = "全屏截取";
	 }
	 
	 private void btnPathScan_Click(object sender, EventArgs e)
        {
            tbFilePath.Text=ChooseFolderPath();
            
        }
        
        //选择保存路径
        public string ChooseFolderPath()
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.RootFolder = System.Environment.SpecialFolder.Desktop;
            fbd.ShowNewFolderButton = true;
            fbd.Description = "请选择目录";
	    if (fbd.ShowDialog() == DialogResult.OK)
            {
                return fbd.SelectedPath.ToString();
            }
            else
            {
                return _filePath;
            }
        }
	 private void tnImageName_TextChanged(object sender, EventArgs e)
        {
            _fileName = tnImageName.Text;
        }
        
         private void tbFilePath_TextChanged(object sender, EventArgs e)
        {
            _filePath = tbFilePath.Text;
        }
        
        //图片格式(默认JPG)
        private void cbImageType_SelectedIndexChanged(object sender, EventArgs e)
        {
            _suffix = cbImageFormat.SelectedItem.ToString().ToLower();
            if (cbImageFormat.SelectedItem.ToString() == ".JPG") _imageFormat = ImageFormat.Jpeg;
            else if (cbImageFormat.SelectedItem.ToString() == ".BMP") _imageFormat = ImageFormat.Bmp;
            else if (cbImageFormat.SelectedItem.ToString() == ".GIF") _imageFormat = ImageFormat.Gif;
            else if (cbImageFormat.SelectedItem.ToString() == ".PNG") _imageFormat = ImageFormat.Png;
            else if (cbImageFormat.SelectedItem.ToString() == ".TIFF") _imageFormat = ImageFormat.Tiff;
            else _imageFormat = ImageFormat.Wmf;
        }
        
        private void MainWindow_MouseUp(object sender, MouseEventArgs e)
        {
            if (_screenShotFlag && _mouseDownFlag)
            {
                _end = e.Location;
                _mouseDownFlag = false;
            }
        }
        
        private void btnScreenShot_Click(object sender, EventArgs e)
        {
            _screenShotFlag = true;
            try
            {
                //隐藏主窗口
                if (cbHideMainWindow.Checked)
                {
                    this.WindowState = FormWindowState.Minimized;
                    this.ShowInTaskbar = false;
                    SetVisibleCore(false);
                }
                Rectangle rect = new Rectangle();
                if (cbCutSize.Text== "全屏截取")
                {
                    rect = Screen.PrimaryScreen.Bounds;                    //获取主显示屏的边界
                }
                else
                {
                    rect = new Rectangle(_start.X, _start.Y, Math.Abs(_end.X - _start.X), Math.Abs(_end.Y - _start.Y));
                }
                Image myImage = new Bitmap(rect.Width, rect.Height);
                Graphics myGraphics = Graphics.FromImage(myImage);     //创建绘图
                myGraphics.CopyFromScreen(new Point(0, 0), new Point(0, 0), rect.Size);
                //保存图片
                string filePathName = string.Concat(_filePath, @"\", _fileName);
                myImage.Save(filePathName, _imageFormat);
                MessageBox.Show("截图成功,图片保存至路径:" + _filePath, "提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show("截图失败;" + ex.Message);
            }
        }
    }
}

在这里插入图片描述
运行效果:
在这里插入图片描述

备注:
截取尺寸中的”框选截取“ 选项暂未开发,功能无效。

.exe文件链接
链接:https://pan.baidu.com/s/17BByWhoy3i7S9Mw1oKOOvg
提取码:58pt
复制这段内容后打开百度网盘手机App,操作更方便哦

后记
笔者才疏学浅,如有错误,望指出。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值