C# DrawToBitmap截取from图的问题,不要推荐我用API!!

来源:百度知道 编辑:UC知道 时间:2024/06/30 23:42:20
截取form窗体的代码:
Bitmap formBitmap = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(formBitmap, new Rectangle(0, 0, this.Width, this.Height));
formBitmap.Save(@"d:\form.bmp", ImageFormat.Bmp);
但是这个代码截取出来有标题栏在里面,怎么能做到一截就把标题栏去掉,只截form的工作区域????先声明,用this.ClientSize.Width一样不行,还是有工作区域!!

Bitmap sourceBitmap = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(sourceBitmap, new Rectangle(0, 0, this.Width, this.Height));
sourceBitmap.Save(@"d:\form2.bmp");

Bitmap resultBitmap = new Bitmap(this.Width, this.ClientSize.Height);
Graphics gr = Graphics.FromImage(resultBitmap);
Rectangle sourceRectangle = new Rectangle(0, this.Height - this.ClientSize.Height, this.Width, this.ClientSize.Height);
Rectangle resultRectangle = new Rectangle(0, 0, this.Width, this.ClientSize.Height);
gr.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);//截取图片中的一部分
resultBitmap.Save(@"d:\form.bmp");

工作区域的矩形应该为
Rectangle(0, this.Height - this.Bottom, this.Width, this.Height)

this.Bottom的描述为:获取控件下边缘与其容器的工作区上边缘之间的距离(以像素为单位)。

Done