FormCaptureScreen.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Runtime.InteropServices;

namespace PictureCopy
{
    public partial class FormCaptureScreen : Form
    {
        public static Image image = null;

        public FormCaptureScreen()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (image != null)
            {
                this.picbox.Image = image;
            }
            else
            {
                this.picbox.Image = null;
                this.picbox.Refresh();
            }
        }

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        public static extern bool BitBlt(
         IntPtr hdcDest, //目标设备的句柄 
         int nXDest, // 目标对象的左上角的X坐标 
         int nYDest, // 目标对象的左上角的X坐标 
         int nWidth, // 目标对象的矩形的宽度 
         int nHeight, // 目标对象的矩形的长度 
         IntPtr hdcSrc, // 源设备的句柄 
         int nXSrc, // 源对象的左上角的X坐标 
         int nYSrc, // 源对象的左上角的X坐标 
         System.Int32 dwRop // 光栅的操作值 
         );

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

        private void aboutButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Code by Star in 2006-7-27,E_mail: fan178@gmail.com ,thank you to use this.");
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            bool isSave = true;
            SaveFileDialog saveImageDialog = new SaveFileDialog();
            saveImageDialog.Title = "Capture screen image save dialog";
            saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";

            if (saveImageDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = saveImageDialog.FileName.ToString();

                if (fileName != "" && fileName != null)
                {
                    string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();

                    System.Drawing.Imaging.ImageFormat imgformat = null;

                    if (fileExtName != "")
                    {
                        switch (fileExtName)
                        {
                            case "jpg":
                                imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                            case "bmp":
                                imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                                break;
                            case "gif":
                                imgformat = System.Drawing.Imaging.ImageFormat.Gif;
                                break;
                            default:
                                MessageBox.Show("只能存取为: jpg,bmp,gif 格式");
                                isSave = false;
                                break;
                        }

                    }

                    //默认保存为JPG格式
                    if (imgformat == null)
                    {
                        imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    }

                    if (isSave)
                    {
                        try
                        {
                            FormCaptureScreen.image.Save(fileName, imgformat);
                            MessageBox.Show("图片已经成功保存!~~");
                        }
                        catch
                        {
                            MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");
                        }
                    }

                }

            }
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            image = null;
            this.Invalidate();
     
        }

        private void mouseButton_Click(object sender, EventArgs e)
        {
            MouseCaptureForm mouseCapture = new MouseCaptureForm();
            mouseCapture.Show();
        }

        private void fullScreenButton_Click(object sender, EventArgs e)
        {
            FormCaptureScreen.image = windowFullScreen();
            picbox.Image = FormCaptureScreen.image;
        }

        public static Bitmap windowFullScreen()
        {
            //建立屏幕Graphics
            Graphics grpScreen = Graphics.FromHwnd(IntPtr.Zero);
            //根据屏幕大小建立位图
            Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, grpScreen);
            //建立位图相关Graphics
            Graphics grpBitmap = Graphics.FromImage(bitmap);
            //建立屏幕上下文
            IntPtr hdcScreen = grpScreen.GetHdc();
            //建立位图上下文
            IntPtr hdcBitmap = grpBitmap.GetHdc();
            //将屏幕捕获保存在图位中
            BitBlt(hdcBitmap, 0, 0, bitmap.Width, bitmap.Height, hdcScreen, 0, 0, 0x00CC0020);
            //关闭位图句柄
            grpBitmap.ReleaseHdc(hdcBitmap);
            //关闭屏幕句柄
            grpScreen.ReleaseHdc(hdcScreen);
            //释放位图对像
            grpBitmap.Dispose();
            //释放屏幕对像
            grpScreen.Dispose();

            //返回捕获位图
            return bitmap;
        }

        private void mouseCaptureBtn_Click(object sender, System.EventArgs e)
        {
            MouseCaptureForm mouseCapture = new MouseCaptureForm();
            mouseCapture.Show();

        }

        private void CaptureScreenForm_Load(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Maximized;
        }
    }
}

        另一个窗体的代码:

MouseCaptureForm.cs

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

namespace PictureCopy
{
    public partial class MouseCaptureForm : Form
    {
        public MouseCaptureForm()
        {
            this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            this.BackgroundImage = FormCaptureScreen.windowFullScreen();
            InitializeComponent();
        }

        private Point pot;
        private Rectangle area = Rectangle.Empty;
        private Image img;
        private int index = -1;
        private string noselectString = "\n.请按下鼠标左键不放拖动选取截图区域\n\n .单击鼠标右键或按ESC取消截图";
        private string selectingString = "\n.松开鼠标左键确定选取范围\n\n.按ESC重新选择";
        private string selectedString = "\n.按鼠标左键调整选择范围\n\n .双击鼠标左键保存截图 \n\n.按鼠标右键重新选择 \n\n.ESC键取消截图";

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            {
                if (this.area == Rectangle.Empty && e.Button == MouseButtons.Left)
                {
                    this.tiptext.Text = selectingString;
                    this.area.Location = new Point(e.X, e.Y);
                }
                this.pot = new Point(e.X, e.Y);
                this.index = this.GetSelectedHandle(new Point(e.X, e.Y));
                this.SetCursor();
            }
        }

        /// <summary> 
        /// 设置鼠标方案 
        /// </summary> 
        private void SetCursor()
        {
            Cursor cr = Cursors.Default;

            if (index == 1 || index == 5)
            {
                cr = Cursors.SizeNWSE;
            }
            else if (index == 2 || index == 6)
            {
                cr = Cursors.SizeNS;
            }
            else if (index == 3 || index == 7)
            {
                cr = Cursors.SizeNESW;
            }
            else if (index == 4 || index == 8)
            {
                cr = Cursors.SizeWE;
            }
            else if (index == 0)
            {
                cr = Cursors.SizeAll;
            }
            Cursor.Current = cr;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawRectangle(new Pen(this.ForeColor), this.area);

            for (int i = 1; i < 9; i++)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.Green), this.GetHandleRect(i));
            }
        }

        private Rectangle GetHandleRect(int index)
        {
            Point point = GetHandle(index);
            return new Rectangle(point.X - 3, point.Y - 3, 6, 6);
        }

        private Point GetHandle(int index)
        {
            int x, y, xCenter, yCenter;

            xCenter = area.X + area.Width / 2;
            yCenter = area.Y + area.Height / 2;
            x = area.X;
            y = area.Y;

            switch (index)
            {
                case 1:
                    x = area.X;
                    y = area.Y;
                    break;
                case 2:
                    x = xCenter;
                    y = area.Y;
                    break;
                case 3:
                    x = area.Right;
                    y = area.Y;
                    break;
                case 4:
                    x = area.Right;
                    y = yCenter;
                    break;
                case 5:
                    x = area.Right;
                    y = area.Bottom;
                    break;
                case 6:
                    x = xCenter;
                    y = area.Bottom;
                    break;
                case 7:
                    x = area.X;
                    y = area.Bottom;
                    break;
                case 8:
                    x = area.X;
                    y = yCenter;
                    break;
            }

            return new Point(x, y);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            int left = area.Left;
            int top = area.Top;
            int right = area.Right;
            int bottom = area.Bottom;
            area.X = Math.Min(left, right);
            area.Y = Math.Min(top, bottom);
            area.Width = Math.Abs(left - right);
            area.Height = Math.Abs(top - bottom);
            this.tiptext.Text = selectedString;
            if (e.Button == MouseButtons.Right)
            {
                if (this.area == Rectangle.Empty)
                {
                    this.Close();
                }
                else
                {
                    this.area = Rectangle.Empty;
                    this.Invalidate();
                }
            }
            this.index = this.GetSelectedHandle(new Point(e.X, e.Y));
            this.SetCursor();
        }

        private int GetSelectedHandle(Point p)
        {
            int index = -1;
            for (int i = 1; i < 9; i++)
            {
                if (GetHandleRect(i).Contains(p))
                {
                    index = i;
                    break;
                }
            }
            if (this.area.Contains(p)) index = 0;

            return index;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (this.Capture)
            {
                this.MoveHandleTo(new Point(e.X, e.Y));
                this.Invalidate();
            }
            else
            {
                this.index = this.GetSelectedHandle(new Point(e.X, e.Y));
                this.SetCursor();
            }
        }

        private void MoveHandleTo(Point point)
        {
            int left = area.Left;
            int top = area.Top;
            int right = area.Right;
            int bottom = area.Bottom;

            switch (index)
            {
                case 0:
                    area.X += point.X - this.pot.X;
                    area.Y += point.Y - pot.Y;
                    this.pot = point;
                    return;
                case 1:
                    left = point.X;
                    top = point.Y;
                    break;
                case 2:
                    top = point.Y;
                    break;
                case 3:
                    right = point.X;
                    top = point.Y;
                    break;
                case 4:
                    right = point.X;
                    break;
                case 5:
                    right = point.X;
                    bottom = point.Y;
                    break;
                case 6:
                    bottom = point.Y;
                    break;
                case 7:
                    left = point.X;
                    bottom = point.Y;
                    break;
                case 8:
                    left = point.X;
                    break;
            }
            this.pot = point;
            area.X = left;
            area.Y = top;
            area.Width = right - left;
            area.Height = bottom - top;
        }

 

        private void MouseCaptureForm_DoubleClick_1(object sender, EventArgs e)
        {
            //修正截取图片过界提示内存不足BUG
            int left = area.Left;
            int right = area.Right;
            int top = area.Top;
            int bottom = area.Bottom;
            if (left < Screen.PrimaryScreen.Bounds.Left)
            { left = Screen.PrimaryScreen.Bounds.Left; }
            if (right > Screen.PrimaryScreen.Bounds.Right)
            { right = Screen.PrimaryScreen.Bounds.Right; }
            if (top < Screen.PrimaryScreen.Bounds.Top)
            { top = Screen.PrimaryScreen.Bounds.Top; }
            if (bottom > Screen.PrimaryScreen.Bounds.Bottom)
            { bottom = Screen.PrimaryScreen.Bounds.Bottom; }
            area.X = left;
            area.Y = top;
            area.Width = right - left;
            area.Height = bottom - top;

            //截取选择区域图片
            Bitmap bm = new Bitmap(this.BackgroundImage);
            this.img = bm.Clone(this.area, System.Drawing.Imaging.PixelFormat.Format16bppArgb1555);
            //Clipboard.SetDataObject(img);
            FormCaptureScreen.image = img;
            //FormCaptureScreen.ActiveForm.Invalidate();
            this.Close();
        }

        private void MouseCaptureForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape && System.Windows.Forms.Button.MouseButtons == MouseButtons.Left)
            {
                this.area = Rectangle.Empty;
                this.Invalidate();
                tiptext.Text = noselectString;
            }
            else if (e.KeyData == Keys.Escape)
            { this.Close(); }
        }

        private void MouseCaptureForm_Load(object sender, EventArgs e)
        {
            tiptext.Text = noselectString;
            
        }

        private void tiptext_MouseMove(object sender, MouseEventArgs e)
        {
            if (tiptext.Location.X == 10 && tiptext.Location.Y == 10)
            {
                Point formloc = new Point((Screen.PrimaryScreen.Bounds.Width - 176), 10);
                tiptext.Location = formloc;
            }
            else
            { tiptext.Location = new Point(10, 10); }
        }
    }
}
posted on 2008-03-22 14:43  Eric Yao  阅读(812)  评论(0编辑  收藏  举报