private void saveControlPicture()
{
int width = this.Size.Width;
int height = this.Size.Height;
using (Bitmap bmp = new Bitmap(width, height))
{
// 把控件读取到Bitmap容器中
this.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
// 文件读取
SaveFileDialog diaglog = new SaveFileDialog();
// 设置初始目录
diaglog.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites).ToString();
// 要保存的文件类型
diaglog.Filter = "(*.jpg)|*.jpg|(*.png)|*.png";
if (diaglog.ShowDialog() == DialogResult.OK)
{
// 已选择的文件路径
string filePath = diaglog.FileName.ToString();
// 保存
bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
MessageBox.Show("保存成功");
}
}
}
this表示当前窗口,指定某个控件,可用指定控件名来代替this