using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
Graphics g;
Bitmap bmp;
public Form3()
{
InitializeComponent();
//主显示器的边框
Rectangle rec= Screen.PrimaryScreen.Bounds;//获取显示器的边框
bmp = new Bitmap(rec.Width, rec.Height);//以显示器的尺寸实例化一张图片
g = Graphics.FromImage(bmp);//以这张图片为基准创建一块画布
}
int i = 0;
private void button1_Click(object sender, EventArgs e)
{
this.ShowInTaskbar = false;//是否在任务栏显示
this.Hide();//隐藏窗体
Thread.Sleep(1);
Size size=new System.Drawing.Size(bmp.Width,bmp.Height);
g.CopyFromScreen(0, 0, 0, 0, size);//将整个屏幕截下来
SaveFileDialog sf = new SaveFileDialog();//实例化一个对话框
sf.Filter = "图片|*.ipg";
sf.FileName = i.ToString() + ".jpg";
i++;
if (sf.ShowDialog()==DialogResult.OK)
{
string fileName = sf.FileName;
bmp.Save(fileName,ImageFormat.Jpeg);
}
this.ShowInTaskbar = true;
this.Show();
}
private void button2_Click(object sender, EventArgs e)
{
this.ShowInTaskbar = false;//是否在任务栏显示
this.Hide();//隐藏窗体
Thread.Sleep(1);
Size size = new System.Drawing.Size(bmp.Width, bmp.Height);
g.CopyFromScreen(0, 0, 0, 0, size);
Form5 f5 = new Form5();//实例化窗体5
f5.BackgroundImage = bmp;//把屏幕的截屏作为窗体的背景
f5.TopMost = true;//让窗体5显示在最前端
f5.FormBorderStyle = FormBorderStyle.None;//设置窗体没有外边框
f5.WindowState = FormWindowState.Maximized;//让窗体最大化覆盖整个屏幕
f5.Show();
}
private void Form3_KeyDown(object sender, KeyEventArgs e)
{
}
}
}
//这里我们用form5来模拟整个屏幕,在form5上面做截屏操作
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form5 : Form
{
Graphics g;//画布
Bitmap bmp;//图片
public Form5()
{
InitializeComponent();
bmp = new Bitmap(this.Width, this.Height);
g = Graphics.FromImage(bmp);//以一个图片来创建一个画布
//g = this.CreateGraphics();
}
Point pStart, pEnd;
private void Form5_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pStart = new Point(e.X, e.Y);
}
}
public Color Color = Color.Red;//画笔的颜色
public int lineWidth = 1;//线条的宽度
private void Form5_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Refresh();
//在这个picture上画一个方框
Graphics g2 = this.CreateGraphics();
pEnd = new Point(e.X, e.Y);//获取释放的点的坐标
Pen p = new Pen(Color, lineWidth);//实例化画具
int x = Math.Min(pStart.X, pEnd.X);//比较两个点的x坐标,返回较小的
int y = Math.Min(pStart.Y, pEnd.Y);//比较两个点的y坐标,返回较小的
int width = Math.Abs(pEnd.X - pStart.X);//获取两个点x坐标的差的绝对值
int height = Math.Abs(pEnd.Y - pStart.Y);//获取两个点y坐标的差的绝对值
g2.DrawRectangle(p, x, y, width, height);
}
}
private void Form5_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pEnd = new Point(e.X, e.Y);//获取释放的点的坐标
Pen p = new Pen(Color, lineWidth);//实例化画具
int x = Math.Min(pStart.X, pEnd.X);//比较两个点的x坐标,返回较小的
int y = Math.Min(pStart.Y, pEnd.Y);//比较两个点的y坐标,返回较小的
int width = Math.Abs(pEnd.X - pStart.X);//获取两个点x坐标的差的绝对值
int height = Math.Abs(pEnd.Y - pStart.Y);//获取两个点y坐标的差的绝对值
g.DrawRectangle(p, x, y, width, height);
this.Refresh();
Bitmap bt = new Bitmap(width, height);
SaveFileDialog sf = new SaveFileDialog();
sf.Filter = "图片|*.jpg";
if (sf.ShowDialog()==DialogResult.OK)
{
string fileName = sf.FileName;
bt.Save(fileName);
this.Close();
Form3 f3 = new Form3();
f3.Show();
}
}
}
}
}