Winform中保存控件区域截屏图片

By | 2017年4月25日

一个很简单的需求,需要保存当前panel绘图区域的截图:

public static void export_screen(Control contr) 
{
    Bitmap bit = new Bitmap(contr.Size.Width, contr.Size.Height);
    Graphics g = Graphics.FromImage(bit);
    g.CompositingQuality = CompositingQuality.HighQuality;
    g.CopyFromScreen(contr.PointToScreen(Point.Empty), Point.Empty, contr.Size);

    SaveFileDialog dia = new SaveFileDialog();
    dia.Filter = "图像文件|*.png";
    dia.Title = "保存图像文件";
    dia.DefaultExt = "png";
    if (dia.ShowDialog() == DialogResult.OK)
    {
        string filename = dia.FileName;
        bit.Save(filename);
    }

    g.Dispose();
    bit.Dispose();
}

使用时,比如右键菜单点击后:

private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
    GraphHelper.export_screen(this);
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

You must enable javascript to see captcha here!