C# 截取指点区域的屏幕,并存为文件,同时在窗口中加载预览

效果如图:
C# 截取指点区域的屏幕,并存为文件,同时在窗口中加载预览
 


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



namespace CopyFromScreen
{
public partial class Form1 : Form
{


public Form1()
{
InitializeComponent();


}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
int width=0;//宽
int height = 0;//高
int x = 0;//起始点x坐标
int y = 0;//起始点y坐标

bool result1 = int.TryParse(this.textBox3.Text,out width);//从文本框获取值
bool result2 = int.TryParse(this.textBox4.Text,out height);
bool result3 = int.TryParse(this.textBox1.Text,out x);
bool result4 = int.TryParse(this.textBox2.Text,out y);

string ScreenshotPath = this.textBox5.Text;//存储路径

try
{
Image image = new Bitmap(width, height);
Graphics g = Graphics.FromImage(image);

g.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height));


string hour = DateTime.Now.Minute.ToString();
string second = DateTime.Now.Second.ToString();
string filepath = ScreenshotPath + "\\" + hour + "_" + second + ".jpg";//生成完整路径
image.Save(filepath);

this.pictureBox1.Image = Image.FromFile(@filepath);//显示到控件预览

}
catch
{

}
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("iexplore.exe", "http://wmnmtm.blog.163.com");
}

private void pictureBox1_Click(object sender, EventArgs e)
{

}
}
}