愛悠閑 > C#屏幕截圖程序源碼

C#屏幕截圖程序源碼

分類: C#  |  標簽: c#,object,image,class  |  作者: liuxianan612 相關  |  發布日期 : 2015-11-23  |  熱度 : 137°

本文屬轉載,版權屬原作者!

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SrceenCapture
{
    public partial class Form1 : Form
    {
        int x=0, y=0, X, Y;
        bool d = false;
        Graphics g;
        Pen p = new Pen(Color.MediumSpringGreen, 1);
        Brush b = new SolidBrush(Color.MediumSpringGreen);
        public Form1()
        {
            InitializeComponent();
        }      
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                x = e.X;
                y = e.Y;
                d = true;
            }
        }
        public void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            X = e.X;
            Y = e.Y;
            if (e.Button == MouseButtons.Left && d)
            {
                System.Threading.Thread.Sleep(20);
                draw();                
            }
        }
        public void draw()
        {
            g = this.CreateGraphics();
            g.Clear(Color.Black);
            g.DrawString(Math.Abs(X - x).ToString() + "  " + Math.Abs(Y - y).ToString(),new Font("Times New Roman",10), b,new Point(x, y-14));
            g.DrawLine(p, Math.Min(x, X), y, (x + X) / 2 - Math.Abs((X - x)) / 6, y);
            g.DrawLine(p, (x + X) / 2 + Math.Abs((x - X)) / 6, y, Math.Max(x, X), y);
            g.DrawLine(p, Math.Min(x, X), Y, (x + X) / 2 - Math.Abs((X - x)) / 6, Y);
            g.DrawLine(p, (x + X) / 2 + Math.Abs((x - X)) / 6, Y, Math.Max(x, X), Y);
            g.DrawLine(p, x, Math.Min(y, Y), x, (y + Y) / 2 - Math.Abs((Y - y)) / 6);
            g.DrawLine(p, x, (y + Y) / 2 + Math.Abs((Y - y)) / 6, x, Math.Max(y, Y));
            g.DrawLine(p, X, Math.Min(y, Y), X, (y + Y) / 2 - Math.Abs((Y - y)) / 6);
            g.DrawLine(p, X, (y + Y) / 2 + Math.Abs((Y - y)) / 6, X, Math.Max(y, Y));
            g.DrawLine(p, (x + X) / 2, 0, (x + X) / 2, Math.Min(y, Y) - (Math.Abs(y - Y)) / 6);
            g.DrawLine(p, 0, (y + Y) / 2, Math.Min(x, X) - Math.Abs((x - X)) / 6, (y + Y) / 2);
            g.DrawLine(p, Math.Max(x, X) + Math.Abs((x - X)) / 6, (y + Y) / 2, 2000, (y + Y) / 2);
            g.DrawLine(p, (x + X) / 2, Math.Max(Y, y) + Math.Abs((y - Y)) / 6, (x + X) / 2, 1000);
            g.DrawLine(p, Math.Abs(x - X) / 2 + Math.Min(x, X) - 7, Math.Abs(y - Y) / 2 + Math.Min(y, Y), Math.Abs(x - X) / 2 + Math.Min(x, X) + 7, Math.Abs(y - Y) / 2 + Math.Min(y, Y));
            g.DrawLine(p, Math.Abs(x - X) / 2 + Math.Min(x, X), Math.Abs(y - Y) / 2 + Math.Min(y, Y) - 7, Math.Abs(x - X) / 2 + Math.Min(x, X), Math.Abs(y - Y) / 2 + Math.Min(y, Y) + 7);
            g.Dispose();
        }
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && x != X && y != Y)
            {
                System.Media.SystemSounds.Asterisk.Play();

                while (this.O                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   pacity > 0)
                {
                    this.Opacity -= 0.1;
                    System.Threading.Thread.Sleep(10);
                }

                Image i = new Bitmap(Math.Abs(X - x) , Math.Abs(Y - y));
                Graphics g = Graphics.FromImage(i);
                g.CopyFromScreen(new Point(Math.Min(x, X), Math.Min(y, Y)), new Point(0, 0),
                                 new Size(Math.Abs(X - x), Math.Abs(Y - y)));
                Clipboard.SetDataObject(i, true);
                i.Dispose(); 
                g.Dispose();
            }
            Application.Exit();
        }
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x1b)
            {
                Application.Exit();
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            while (this.Opacity > 0)
            {
                this.Opacity -= 0.05;
                System.Threading.Thread.Sleep(10);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            while (this.Opacity < 0.6)
            {
                this.Opacity += 0.1;
                System.Threading.Thread.Sleep(10);
            }
        }
    }
}


快乐彩中奖说明