2 回答

TA貢獻1866條經驗 獲得超5個贊
我不確定您是如何使用當前代碼復制屏幕的正確部分的。Bounds ()屬性返回一個相對于PARENT控件的矩形,但您要求控件本身(而不是父控件)轉換為屏幕坐標:
獲取或設置控件(包括其非客戶端元素)相對于父控件的大小和位置(以像素為單位)。
我希望看到更多類似的東西:
Rectangle bounds = ctrl.Parent.RectangleToScreen(ctrl.Bounds);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(bounds.Location, new Point(0,0), bounds.Size);
}
不過,我不知道這是否能在不同的縮放模式下正常工作。

TA貢獻1852條經驗 獲得超7個贊
我找不到修復它的方法。我更改了方法并在表單上使用了 DrawToBitmap,然后從控件位置制作了一個圖像。我必須考慮一個固定的偏移量。我認為這與頂部欄包含在窗體的位圖中而不是包含在窗體中控件的位置有關。
要考慮的一件事是 DrawToBitmap 以相反的堆棧順序繪制。如果您有疊加對象,您可能需要顛倒 DrawToBitmap 的順序。
private void capture(Control ctrl, string fileName)
{
Bitmap bitmapForm = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bitmapForm, new Rectangle(0, 0, this.Width, this.Height));
Rectangle myControlRect = new Rectangle(ctrl.Location,ctrl.Size);
//Correct for boarder around form
myControlRect.Offset(8,31);
Bitmap bitmap = bitmapForm.Clone(myControlRect, PixelFormat.DontCare);
string filetype = fileName.Substring(fileName.LastIndexOf('.')).ToLower();
switch (filetype)
{
case ".png":
bitmap.Save(fileName, ImageFormat.Png);
break;
case ".jpeg":
bitmap.Save(fileName, ImageFormat.Jpeg);
break;
case ".bmp":
bitmap.Save(fileName, ImageFormat.Bmp);
break;
default:
break;
}
}
- 2 回答
- 0 關注
- 169 瀏覽
添加回答
舉報