用C#写屏幕截图小程序

一 方法

   主要是利用Graphics.CopyFromScreen 方法实现屏幕截图。

二 实现

  1 建一个WindowsForm程序。Form1

     界面如图:

Form代码:

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

namespace PictureCutForm
{
    public partial class Form1 : Form
    {
        private string fullFileName;
        private GForm gForm;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                if (gForm != null)
                {
                    fullFileName = gForm.FileFullName;
                    Process p = new Process();
                    ProcessStartInfo startInfo = new ProcessStartInfo(fullFileName);
                    p.StartInfo = startInfo;
                    p.Start();
                }
                else
                {
                    MessageBox.Show("您还没有截图");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (gForm != null)
                {
                    fullFileName = gForm.FileFullName;
                    FileInfo file = new FileInfo(fullFileName);
                    file.Delete();
                    MessageBox.Show("删除成功");
                }
                else
                {
                    MessageBox.Show("您还没有截图!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnCutPicture_Click(object sender, EventArgs e)
        {
            gForm = new GForm();
            gForm.Show();
        }
        


       
    }
}


二 再建一个WindoesFrom窗体GForm,用于实现截图。对窗体的属性作如下设置:

this.Opacity = 0;
this.ShowIcon = false;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.ControlBox = false;
this.BackColor = System.Drawing.Color.White;
 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

GForm代码:

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.Threading;
using System.Windows.Forms;
using System.Diagnostics;

namespace PictureCutForm
{
    public partial class GForm : Form
    {
        private int start_x, start_y;   //起始坐标
        private int end_x, end_y;    //终止坐标
        private int width_x, height_y; //长和宽
        private Rectangle rect;   //矩形框
        private Image image;
        private Graphics g;

        private string fileName;
        private string fullFileName;
        private bool isMouseDown = false;
        
        public GForm( )
        {
            InitializeComponent();
            fileName=fullFileName;
        }

        //鼠标按下事件
        private void GForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = true;
               // g = this.CreateGraphics();
                start_x = e.X;     //按下鼠标时的坐标
                start_y = e.Y;
            }
        }

        private void GForm_MouseUp(object sender, MouseEventArgs e)
        {
            end_x = e.X;
            end_y = e.Y;
            width_x = Math.Abs(end_x - start_x);
            height_y = Math.Abs(end_y - start_y);
            rect = new Rectangle(start_x, start_y, width_x, height_y);
            image=new Bitmap(width_x,height_y);
            g = Graphics.FromImage(image);
           // g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(width_x, height_y));
            g.CopyFromScreen(new Point(start_x,start_y),new Point(0,0),new Size(width_x,height_y));
            fileName = GetFileName();
            string path=@"C:\Documents and Settings\Administrator\桌面\";
            fullFileName = path + fileName+".jpg";
            image.Save(fullFileName,ImageFormat.Jpeg);
            this.Close();
            if (MessageBox.Show("截图成功,是否打开图片", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Process p = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo(fullFileName);
                p.StartInfo = startInfo;
                p.Start();
               
            }
           
        }

        /// <summary>
        /// 获取文件名
        /// </summary>
        /// <returns></returns>
        public string GetFileName()
        {
            string fileName = DateTime.Now.ToString();
            fileName= fileName.Replace("/", "");
            fileName= fileName.Replace(" ","");
            fileName = fileName.Replace(":", ""); 
            fileName=fileName.Replace("\\", "");
            Random rand = new Random();
            int ran = rand.Next(0, 1000);
            fileName += ran.ToString();
            return fileName;
        }

        private void GForm_MouseMove(object sender, MouseEventArgs e)
        {
            //如果是鼠标左键并且先曾按下
            if (e.Button == MouseButtons.Left && isMouseDown == true)
            {
                g = this.CreateGraphics();
                
                Pen p=new Pen (Brushes.Black,2);
                g.DrawRectangle(p, start_x, start_y, Math.Abs(e.X - start_x), Math.Abs(e.Y - start_y));
                //g.Flush();
                
                g.Clear(Color.White);
                
            }
        }

        private void GForm_Load(object sender, EventArgs e)
        {
            this.Opacity = 0.5;
            this.BackColor = Color.SteelBlue;
        }

        public string FileFullName
        {
            get { return this.fullFileName; }
        }

    }

   
}



 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值