winform简单截图

//该代码实现简单截图,鼠标左键按下开始截图,移动过程中红线方框确定截图范围,左键放开时弹出保存对话框,选好路径保存就OK。

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.Windows.Forms;

namespace chartdemo
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        Point startPoint = new Point();
        Point endPoint = new Point();
        int width;
        int height;
        private void Form4_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                startPoint = PointToScreen(e.Location);
            }
        }

        private void Form4_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Graphics g = this.CreateGraphics();
                Pen p = new Pen(Color.Red, 1);
                this.Refresh();
                Point tP = new Point();
                tP = proccessPoint(startPoint,PointToScreen(e.Location));
                g.DrawRectangle(p, tP.X-this.Location.X-4, tP.Y-this.Location.Y-30, width, height);
            }
        }


        bool flag = false;
        private void Form4_MouseUp(object sender, MouseEventArgs e)
        {
            endPoint = PointToScreen(e.Location);
            startPoint = proccessPoint(startPoint, endPoint);
            if (flag)
            {
                IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);//创建显示器的DC
                Graphics g1 = Graphics.FromHdc(dc1);//由句柄创建Graphics
                Bitmap MyImage = new Bitmap(width, height, g1);//创建与屏幕同大小的Bitmap
                Graphics g2 = Graphics.FromImage(MyImage);//获得屏幕的句柄
                IntPtr dc3 = g1.GetHdc();//获得位图的句柄
                IntPtr dc2 = g2.GetHdc();//把当前屏幕捕获到位图中
                BitBlt(dc2, 0, 0, width, height, dc3, startPoint.X, startPoint.Y, 13369376);//把当前屏幕拷贝到位图中
                g1.ReleaseHdc(dc3);//释放屏幕句柄
                g2.ReleaseHdc(dc2);
                string filePath = null;
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.RestoreDirectory = true;//打开上一次所打开的目录
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    filePath = saveFileDialog1.FileName.ToString()+".jpeg";
                }
                MyImage.Save(filePath, ImageFormat.Jpeg);
            }
            else
            {
                MessageBox.Show("错误");
            }

        }

        public Point proccessPoint(Point sP,Point eP)
        {
            width = eP.X - sP.X;
            height = eP.Y - sP.Y;
            if (width == 0 || height == 0)
            {
                flag = false;
            }
            else if (width > 0 && height > 0)
            {
                flag = true;
            }
            else if (width > 0 && height < 0)
            {
                sP.Y += height;
                height = Math.Abs(height);
                flag = true;
            }
            else if (width < 0 && height > 0)
            {
                sP.X += width;
                width = Math.Abs(width);
                flag = true;
            }
            else
            {
                sP.X += width;
                sP.Y += height;
                height = Math.Abs(height);
                width = Math.Abs(width);
                flag = true;
            }
            return sP;
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            width = 0;
            height = 0;
        }
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private 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   //   光栅的操作值
            );

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern IntPtr CreateDC(
            string lpszDriver,   //   驱动名称   
            string lpszDevice,   //   设备名称   
            string lpszOutput,   //   无用,可以设定位"NULL"   
            IntPtr lpInitData   //   任意的打印机数据   
            );
    }
}


你可能感兴趣的:(object,String,null,Class,WinForm)