如何在C#中捕获表单客户端到图像文件? [英] How to capture client of form to image file in C#?
问题描述
大家好,
我有一个问题:我需要在C#中捕获Form到图像文件。这是我的代码,但它不能正常工作,我想只获得客户区,而不是获得另一个形式的区域:
public void GhiImage()
{
try
{
SaveFileDialog SvDialog = new SaveFileDialog();
SvDialog.Title =Ghi thanh file anh!;
SvDialog.DefaultExt =bmp;
SvDialog.Filter =Ảnh位图(* .bmp)| * .bmp |Ảnhjpg(* .jpg)| * .jpg;
SvDialog.FilterIndex = 1;
if(SvDialog.ShowDialog()== DialogResult.OK)
{
Graphics g = this.CreateGraphics();
位图bmp =新位图(this.ClientRectangle.Width,this.ClientRectangle.Height);
this.DrawToBitmap(bmp,this.ClientRectangle);
bmp.Save(SvDialog.FileName);
bmp.Dispose();
}
}
捕获
{
}
}
它包含所有表单(边框和客户区)。现在帮我修复我的代码以完全正常工作。谢谢大家。
我尝试了什么:
我编码,但它uncorrectly工作
这两行:
<预LANG = C#> BMP位图= new 位图(此 .ClientRectangle.Width,此 .ClientRectangle.Height);
<跨度类= 代码关键字>此跨度> .DrawToBitmap(BMP,<跨度类= 代码关键字>此跨度> .ClientRectangle);允许您指定要绘制到位图的客户区的哪个部分。在这里它确实想要你要求:它绘制整个客户区。如果你只想要它的一部分,你必须定义一个界定这个部分的矩形,并使用这个矩形而不是
ClientRectangle
。
Hi all,
I have a problem: I need to capture of Form to image file in C#. This is my code to do this, but it is not to work correctly, I want to get only client area, not get another area of form:
public void GhiImage() { try { SaveFileDialog SvDialog = new SaveFileDialog(); SvDialog.Title = "Ghi thanh file anh!"; SvDialog.DefaultExt = "bmp"; SvDialog.Filter = "Ảnh bitmap (*.bmp)|*.bmp |Ảnh jpg (*.jpg)|*.jpg"; SvDialog.FilterIndex = 1; if (SvDialog.ShowDialog() == DialogResult.OK) { Graphics g = this.CreateGraphics(); Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height); this.DrawToBitmap(bmp, this.ClientRectangle); bmp.Save(SvDialog.FileName); bmp.Dispose(); } } catch { } }
It contains all of Form (border and client area). Now help me fix my code to work exactly. Thanks you, everyone.
What I have tried:
I coded it, but it worked uncorrectly!
Both these lines:
Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height); this.DrawToBitmap(bmp, this.ClientRectangle);allow you to specify which part of the client area you want to draw to your bitmap. Here it does exactly want you asked for: it draws the entire client area. If you want only a portion of it, you have to define a rectangle delimiting this portion, and use this rectangle instead of
ClientRectangle
.
这篇关于如何在C#中捕获表单客户端到图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!