C#截取窗体指定区域图片

程序窗体:

想要实现的效果就是点击保存后,只截取图片的区域,效果如下:

实现代码如下:

private void button3_Click(object sender, EventArgs e)
        {
            int with = this.Width;
            int height = this.Height - panel1.Height;
            Image im = new Bitmap(with, height);
            Graphics g = Graphics.FromImage(im);
            g.CopyFromScreen(this.PointToScreen(new Point(0, panel1.Height)), new Point(0, 0), new Size(with, height));
            //第一个参数是取屏幕左上角,第二个参数是设置画布im中的对应坐标,第三个参数是大小了。

            saveFileDialog1.Filter = "Image(*.jpg)|*.jpg";
            saveFileDialog1.ShowDialog();
            if (saveFileDialog1.FileName != "")
            {
                //Bitmap bt = new Bitmap(with, height);
                //this.DrawToBitmap(bt,new Rectangle( 200,200,100,100));  //这种方法设置的起点参数无效,只有长度和宽度有效
                //bt.Save(saveFileDialog1.FileName);
                //bt.Dispose();
                im.Save(saveFileDialog1.FileName);
            }
            g.Dispose();
            im.Dispose();
        }