首先选择保存图片的路径:
复制代码 代码如下:
saveFileDialog1.Title = "保存";
saveFileDialog1.Filter = "*.png|*.png";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
timer1.Enabled = true;
}
其次保存控件图片(为了等待savedialoge控件完全关闭,使用timer延缓):
复制代码 代码如下:
timer1.Enabled = false;
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));//保存整个窗体为图片
g.CopyFromScreen(chartType.PointToScreen(Point.Empty), Point.Empty, chartType.Size);//只保存某个控件
bit.Save(saveFileDialog1.FileName);//默认保存格式为PNG,保存成jpg格式质量不是很好
if (File.Exists(saveFileDialog1.FileName.ToString()))
{
MessageBox.Show("截图成功!");
return;
}