c# screen
时间: 2023-07-27 07:13:25 浏览: 61
您好!如果您想要在 C# 中截屏,可以使用 .NET Framework 提供的 `System.Drawing` 命名空间下的 `Bitmap` 和 `Graphics` 类。以下是一个简单的示例代码:
```csharp
using System.Drawing;
using System.Windows.Forms;
private void CaptureScreen()
{
// 获取屏幕大小
Rectangle bounds = Screen.GetBounds(Point.Empty);
// 创建一个与屏幕大小相同的图像
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
// 创建一个用于绘制图像的图形对象
using (Graphics g = Graphics.FromImage(bitmap))
{
// 将屏幕绘制到图像上
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
// 保存图像
bitmap.Save("screenshot.png", ImageFormat.Png);
}
}
```
该代码将捕获整个屏幕并保存为 PNG 图像。您可以根据需要进行修改以满足您的需求。
相关问题
c# 截屏
在C#中,可以使用以下代码实现截屏功能:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
public static class ScreenShot
{
public static void TakeScreenShot(string fileName)
{
// 获取屏幕大小
Size screenSize = Screen.PrimaryScreen.Bounds.Size;
// 创建一个与屏幕大小相同的图像
using (Bitmap bmp = new Bitmap(screenSize.Width, screenSize.Height))
{
// 将屏幕图像拷贝到位图中
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), screenSize);
}
// 保存图像到文件
bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
```
使用时,可以调用 `TakeScreenShot` 方法并传入需要保存的文件名,例如:
```csharp
ScreenShot.TakeScreenShot("screenshot.png");
```
C#窗口居中
要让C#程序中的窗口居中,可以通过代码设置窗口的位置。
以下是一段示例代码,可以在窗口加载时居中窗口:
```
private void Form1_Load(object sender, EventArgs e)
{
// 计算居中位置
int x = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
int y = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;
// 设置窗口位置
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(x, y);
}
```
在这段代码中,使用了 `Screen.PrimaryScreen.WorkingArea` 属性获取屏幕的工作区域,然后通过计算,得出窗口应该显示的位置。最后,将窗口的位置设置为计算得出的位置即可。