winForm截图,截当前整个窗体和某个区域

首次发布:2017-08-14 15:37
//截取整个窗体 
private void button1_Click(object sender, EventArgs e)
 {
	Bitmap bit=new Bitmap(this.Width,this.Height);//实例化一个和窗体一样大的bitmap
	Graphics g=Graphics.FromImage(bit);
	g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
	g.CopyFromScreen(this.Left,this.Top,0,0,new Size(this.Width,this.Height));//保存整个窗体为图片		
	bit.Save("weiboTemp.png");//默认保存格式为PNG,保存成jpg格式质量不是很好
 }

//截取窗体的某个区域如  panel1这一个区域
private void button2_Click(object sender, EventArgs e)
{
	Bitmap bit=new Bitmap(this.panel1.Width,this.panel1.Height);//实例化一个和窗体一样大的bitmap
	Graphics g=Graphics.FromImage(bit);
	g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
	g.CopyFromScreen(panel1 .PointToScreen(Point.Empty), Point.Empty, panel1.Size);//只保存某个控件(这里是panel1)
	bit.Save("weiboTemp.png");//默认保存格式为PNG,保存成jpg格式质量不是很好
}