用.NET语言写一段将本窗口界面截图保存到运行目录的代码
时间: 2024-05-08 19:15:42 浏览: 91
以下是将本窗口界面截图保存到运行目录的C#代码:
```csharp
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace MyApplication
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void btnSaveScreenshot_Click(object sender, EventArgs e)
{
Bitmap screenshot = new Bitmap(this.Width, this.Height);
Graphics graphics = Graphics.FromImage(screenshot);
graphics.CopyFromScreen(this.Left, this.Top, 0, 0, this.Size);
string directory = AppDomain.CurrentDomain.BaseDirectory;
string filename = "screenshot.png";
string filepath = Path.Combine(directory, filename);
screenshot.Save(filepath, System.Drawing.Imaging.ImageFormat.Png);
MessageBox.Show("Screenshot saved as " + filename);
}
}
}
```
在窗口中添加一个按钮,并将按钮的Click事件绑定到btnSaveScreenshot_Click方法上。当用户点击该按钮时,该方法将创建一个Bitmap对象,并复制当前窗口的图像到该对象中。然后,该方法将将Bitmap对象保存为PNG文件,并显示一个消息框,指示截图已保存。
阅读全文