基本信息
源码名称:C#自己封装,含windowsapi封装
源码大小:0.09M
文件格式:.rar
开发语言:C#
更新时间:2016-04-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 5 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using XBC;
using System.IO;

namespace XBC
{
    /// <summary>
    /// 用于截图的类
    /// </summary>
    public partial class frmShow : Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public frmShow() {
            InitializeComponent();
            BackgroundImage = ControlComputer.CaptrueAllScreen(false);
            backImage = BackgroundImage as Bitmap;
            Init();
        }

        FromState _State;
        Point one;
        Point two;
        Rectangle rec;
        Bitmap backImage;
        Bitmap result;
        /// <summary>
        /// 获取得到的图片的Bitmap对象
        /// </summary>
        public Bitmap Result {
            get { return result; }
        }

        #region 方法区

        private void Init() {
            ChangLocation();
            Stream s = new MemoryStream(Properties.Resources.a);
            //  byte[] bs =;
            //for (int i = 0; i < bs.Length - 1;   i) {
            //    s.WriteByte(bs[i]);
            //}            

            Cursor = new Cursor(s);
            _State = FromState.Start;
            lblStata.Text = "请单击选择第一个点";

        }

        private void ChangLocation() {
            pnlInfo.Left = this.Width - pnlInfo.Width;
            pnlInfo.Top = this.Height - pnlInfo.Height;
        }

        private void DrawRectangle() {
            Graphics g = this.CreateGraphics();
            g.DrawRectangle(Pens.Red, rec);
        }

        private void ShowInfo() {
            lblLeft.Text = "";
            lblTop.Text = "";
            lblWidth.Text = "";
            lblHeight.Text = "";
        }

        private void ShowInfo(Rectangle value) {
            lblLeft.Text = value.Left.ToString();
            lblTop.Text = value.Top.ToString();
            lblWidth.Text = value.Width.ToString();
            lblHeight.Text = value.Height.ToString();
        }

        private void ShowImage(Rectangle temp) {
            if (temp.Width != 0 && temp.Height != 0) {
                Graphics g = picBig.CreateGraphics();
                g.DrawImage(backImage.Clone(temp, PixelFormat.DontCare), 0, 0, picBig.Width, picBig.Height);
            }
        }

        //刷新区域
        private void KeyInvalidate(Rectangle rec) {
            Rectangle temp = rec;
            temp.Height  ;
            temp.Width  ;
            Invalidate(temp);
        }
        #endregion

        private void frmShow_SizeChanged(object sender, EventArgs e) {
            ChangLocation();
        }

        private void pnlInfo_MouseEnter(object sender, EventArgs e) {
            if (pnlInfo.Top == this.Height - 300) {
                pnlInfo.Top = 0;
            }
            else {
                pnlInfo.Top = this.Height - 300;
            }
        }

        private void frmShow_MouseMove(object sender, MouseEventArgs e) {
            if (_State == FromState.Start || _State == FromState.One) {
                Rectangle temp = new Rectangle();
                temp.X = e.Location.X - 10;
                temp.Y = e.Location.Y - 10;
                temp.Width = 20;
                temp.Height = 20;
                CheckTempRectangle(ref  temp);
                ShowImage(temp);
            }
        }

        //选择图像
        private void frmShow_MouseDown(object sender, MouseEventArgs e) {
            if (_State == FromState.Start) {
                if (e.Button == MouseButtons.Left) {
                    one = e.Location;
                    lblLeft.Text = one.X.ToString();
                    lblTop.Text = one.Y.ToString();
                    lblStata.Text = "已选择第一个点";
                    _State  ;
                }
                else if (e.Button == MouseButtons.Right) {
                    this.Close();
                }
            }
            else if (_State == FromState.One) {
                if (e.Button == MouseButtons.Left) {
                    two = e.Location;
                    rec.X = Math.Min(one.X, two.X);
                    rec.Y = Math.Min(one.Y, two.Y);
                    rec.Width = Math.Abs(one.X - two.X);
                    rec.Height = Math.Abs(one.Y - two.Y);
                    ShowInfo(rec);
                    lblStata.Text = "已选择了区域";
                    DrawRectangle();
                    ShowImage(rec);
                    _State  ;
                }
                else if (e.Button == MouseButtons.Right) {
                    _State--;
                    lblStata.Text = "已选择第一个点";
                }
            }
            else if (_State == FromState.Two) {
                if (e.Button == MouseButtons.Right) {
                    rec.Height  ;
                    rec.Width  ;
                    Invalidate(rec);
                    _State -= 2;
                    ShowInfo();
                    lblStata.Text = "请单击选择第一个点";
                }
            }
        }

        //改变位置和大小
        private void frmShow_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Up) {
                KeyInvalidate(rec);
                Update();
                if (e.Shift) {
                    rec.Height--;
                }
                else {
                    rec.Y--;
                }
                DrawRectangle();
            }
            else if (e.KeyCode == Keys.Down) {
                KeyInvalidate(rec);
                Update();
                if (e.Shift) {
                    rec.Height  ;
                }
                else {
                    rec.Y  ;
                }
                DrawRectangle();
            }
            else if (e.KeyCode == Keys.Left) {
                KeyInvalidate(rec);
                Update();
                if (e.Shift) {
                    rec.Width--;
                }
                else {
                    rec.X--;
                }
                DrawRectangle();
            }
            else if (e.KeyCode == Keys.Right) {
                KeyInvalidate(rec);
                Update();
                if (e.Shift) {
                    rec.Width  ;
                }
                else {
                    rec.X  ;
                }
                DrawRectangle();
            }
            else if (e.KeyCode == Keys.Enter) {
                if (rec.Width != 0 && rec.Height != 0) {
                    result = backImage.Clone(rec, PixelFormat.DontCare);
                    //frmSelect objSelect = new frmSelect();
                    //DialogResult d = objSelect.ShowDialog();
                    //if (d == DialogResult.Yes) {
                    //    Clipboard.SetImage(b);
                    //}
                    //else if (d == DialogResult.No) {
                    //    if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
                    //        b.Save(saveFileDialog1.FileName,ImageFormat.Bmp);
                    //    }
                    //}
                }
                this.Close();
                this.DialogResult = DialogResult.OK;
                return;
            }
            CheckRectangle();
            ShowInfo(rec);
            ShowImage(rec);
        }

        private void CheckRectangle() {
            if (rec.X < 0)
                rec.X = 0;
            if (rec.Y < 0)
                rec.Y = 0;
            if (rec.X > 1280)
                rec.X = 1280;
            if (rec.Y > 800)
                rec.Y = 800;
            if (rec.Width < 0)
                rec.Width = 0;
            if (rec.Height < 0)
                rec.Height = 0;
            if (rec.Width   rec.X > 1280)
                rec.Width = 1280 - rec.X;
            if (rec.Height   rec.Y > 1280)
                rec.Height = 800 - rec.Y;
        }

        private void CheckTempRectangle(ref  Rectangle value) {
            if (value.X < 0)
                value.X = 0;
            if (value.Y < 0)
                value.Y = 0;
            if (value.Width   value.X > 1280)
                value.Width = 1280 - value.X;
            if (value.Height   value.Y > 800)
                value.Height = 800 - value.Y;
        }

    }

    internal enum FromState
    {
        /// <summary>
        /// 表示窗体此时刚进入截图状态
        /// </summary>
        Start,
        /// <summary>
        /// 表示现在已选定了一个点
        /// </summary>
        One,
        /// <summary>
        /// 表示现在已选定了二个点
        /// </summary>
        Two,
    }
}