c#实现屏幕截图

c#提供了System.Drawing命名空间中的类,可以很方便地实现屏幕截图功能,以下是一个示例代码:

```csharp
using System;
using System.Drawing;
using System.Windows.Forms;




namespace ScreenShotExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }




        private void btnScreenshot_Click(object sender, EventArgs e)
        {
            // 获取屏幕大小
            Rectangle bounds = Screen.GetBounds(Point.Empty);




            // 创建一个bitmap对象并将其设置为屏幕大小
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                // 创建一个Graphics对象并将其设置为bitmap
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // 将整个屏幕绘制到Graphics对象中
                    g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
                }




                // 将截图保存为jpg文件
                bitmap.Save("screenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }




            MessageBox.Show("屏幕截图已保存");
        }
    }
}
```

在上面的代码中,我们首先使用Screen.GetBounds方法获取当前屏幕的大小。然后,创建一个Bitmap对象,并将其大小设置为屏幕大小。接着,我们创建一个Graphics对象,并将其设置为Bitmap。最后,使用Graphics.CopyFromScreen方法将整个屏幕绘制到Graphics对象中。最后保存为jpg文件。

这个示例只保存了一张屏幕截图。如果需要截取窗口或指定区域,可以使用GetWindowRect或GetDesktopWindow方法获取窗口的矩形区域,或使用一些计算方法获取指定矩形的截图。

需要注意的是,保存的截图可能会很大,因此最好使用压缩算法将其保存为较小的文件。另外,由于使用了System.Drawing命名空间,因此需要添加对System.Drawing.dll的引用。

  • END
  • 如果觉得好用欢迎大家分享给身边的小伙伴!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 C# 实现框选截图功能的代码示例: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class ScreenCapture : Form { private Point start; private Rectangle rect; public ScreenCapture() { this.DoubleBuffered = true; this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; this.Cursor = Cursors.Cross; this.MouseDown += ScreenCapture_MouseDown; this.MouseMove += ScreenCapture_MouseMove; this.MouseUp += ScreenCapture_MouseUp; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); using (Pen pen = new Pen(Color.Red, 2)) { e.Graphics.DrawRectangle(pen, rect); } } private void ScreenCapture_MouseDown(object sender, MouseEventArgs e) { start = new Point(e.X, e.Y); rect = new Rectangle(start, Size.Empty); } private void ScreenCapture_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point current = new Point(e.X, e.Y); rect.Location = new Point( Math.Min(start.X, current.X), Math.Min(start.Y, current.Y)); rect.Size = new Size( Math.Abs(start.X - current.X), Math.Abs(start.Y - current.Y)); Invalidate(); } } private void ScreenCapture_MouseUp(object sender, MouseEventArgs e) { if (rect.Width > 0 && rect.Height > 0) { using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height)) { using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.CopyFromScreen(rect.Location, Point.Empty, rect.Size); } Clipboard.SetImage(bitmap); MessageBox.Show("截图已复制到剪贴板。"); } } Close(); } public static void Main() { Application.Run(new ScreenCapture()); } } ``` 这个程序创建了一个全屏的窗体,并在窗体上绘制了一个红色的矩形,用于框选截图。当用户按下鼠标左键并拖动时,矩形会跟随鼠标移动。当用户释放鼠标左键时,程序会将框选区域的屏幕截图复制到剪贴板,并关闭窗体。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值