C# 截图工具winform_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C# 截图工具winform

C# 截图工具winform

 2017/7/30 9:38:41  雨的气息  程序员俱乐部  我要评论(0)
  • 摘要:做了好几天,对象比较多,弄的都是乱的,最后还是一个个试出来的。BitmapGraphicsFromImagePenRectangleDrawRectangle真的混乱,看了好几天,才明白参数之间的关系。下载pan.baidu.com/s/1c2CMRhY思路:form1就是界面,全屏截图。form2就区域截图Form1代码inti=0;intw=Screen.PrimaryScreen.Bounds.Width;inth=Screen.PrimaryScreen.Bounds.Height
  • 标签:C# for 工具 winform

做了好几天,对象比较多,弄的都是乱的,最后还是一个个试出来的。

Bitmap

Graphics

FromImage

Pen

Rectangle

DrawRectangle

真的混乱,看了好几天,才明白参数之间的关系。

下载     pan.baidu.com/s/1c2CMRhY

 

思路:form1 就是界面,全屏截图。form2 就区域截图

Form1代码

int i = 0;
        int w = Screen.PrimaryScreen.Bounds.Width;
        int h = Screen.PrimaryScreen.Bounds.Height;

        private void button1_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
            Thread.Sleep(100);
            i += 1;
            Bitmap b = new Bitmap(w,h);
            Graphics g = Graphics.FromImage(b);
            g.CopyFromScreen(0,0,0,0,new Size(w,h));
            b.Save("C:/Users/Administrator/Desktop/截图小王子"+i+".jpg");
            WindowState = FormWindowState.Normal;
            Clipboard.SetImage(b);
            this.Cursor = Cursors.Default;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("C:/Users/Administrator/Desktop");
            TopMost = false;
            this.Cursor = Cursors.Default;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form2 f2 = new Form2();
            f2.Show();
        }

 

Form2 代码

 

int i = 0;
        Bitmap bit;
        Bitmap bit2;
        Bitmap copy;
        int x;
        int y;
        int xx;
        int yy;
        Pen pen = new Pen(Color.Red,2);
        Rectangle rect = new Rectangle();
        Graphics g2;

        private void Form2_Load(object sender, EventArgs e)
        {
            i++;
            //Opacity = 0.5;
            WindowState = FormWindowState.Minimized;
            FormBorderStyle = FormBorderStyle.None;
            this.Cursor = Cursors.Cross;
            int w = Screen.PrimaryScreen.Bounds.Width;
            int h = Screen.PrimaryScreen.Bounds.Height;
            bit = new Bitmap(w, h);//获取桌面图
            Graphics g = Graphics.FromImage(bit);//把图画出来
            g.CopyFromScreen(0, 0, 0, 0, new Size(w, h));
            pictureBox1.Image = bit;
            bit.Save("C:/Users/Administrator/Desktop/区域截图" + i + ".jpg");
            WindowState = FormWindowState.Maximized;

        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Right)
            {
                this.Hide();
                Form1 f1 = new Form1();
                f1.Show();
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            x = Math.Abs(e.X);
            y = Math.Abs(e.Y);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                xx = Math.Abs(e.X);
                yy = Math.Abs(e.Y);
                Graphics g = pictureBox1.CreateGraphics();
                rect = new Rectangle(x, y, xx - x, yy - y);
                Refresh();
                g.DrawRectangle(pen, rect);
                
            }
        }
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                copy = (Bitmap)bit.Clone();
                bit2 = new Bitmap(rect.Width, rect.Height);
                g2 = Graphics.FromImage(bit2);
                g2.DrawImage(copy, new Rectangle(0, 0, rect.Width, rect.Height), rect, GraphicsUnit.Pixel);
                pictureBox1.Image = bit2;
                bit2.Save("C:/Users/Administrator/Desktop/区域截图" + i + ".jpg");
                Clipboard.SetImage(bit2);
            }

 

发表评论
用户名: 匿名