C# 截图ScreenCapture,保存
简化版:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; using System.Windows.Forms; using System.IO; using System.Drawing.Imaging; namespace ConsoleApp346 { class Program { [STAThread] static void Main(string[] args) { ScreenCapture(); } static void ScreenCapture() { Rectangle bounds = Screen.GetBounds(System.Drawing.Point.Empty); using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) { using (Graphics g = Graphics.FromImage(bitmap)) { g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, bounds.Size); } string fullName = Directory.GetCurrentDirectory() + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg"; bitmap.Save(fullName, ImageFormat.Jpeg); } } } }
效果如图:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
namespace ConsoleApp346
{
class Program
{
[STAThread]
static void Main(string[] args)
{
ScreenCapture();
}
static void ScreenCapture()
{
Rectangle bounds = Screen.GetBounds(System.Drawing.Point.Empty);
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, bounds.Size);
}
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Title = "Save Pictures";
sfd.InitialDirectory = Directory.GetCurrentDirectory();
sfd.RestoreDirectory = true;
sfd.Filter = "bmp files(*.bmp)|*.bmp|All Files(*.*)|*.*";
sfd.FileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg";
if (sfd.ShowDialog() == DialogResult.OK)
{
bitmap.Save(sfd.FileName, ImageFormat.Jpeg);
}
}
}
}
}
}
效果如下所示:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Drawing; 7 using System.Windows.Forms; 8 using System.IO; 9 using System.Drawing.Imaging; 10 11 namespace ConsoleApp346 12 { 13 class Program 14 { 15 [STAThread] 16 static void Main(string[] args) 17 { 18 ScreenCapture(); 19 20 } 21 22 static void ScreenCapture() 23 { 24 Rectangle bounds = Screen.GetBounds(System.Drawing.Point.Empty); 25 using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) 26 { 27 using (Graphics g = Graphics.FromImage(bitmap)) 28 { 29 g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, bounds.Size); 30 } 31 32 using (SaveFileDialog sfd = new SaveFileDialog()) 33 { 34 sfd.Title = "Save Pictures"; 35 sfd.InitialDirectory = Directory.GetCurrentDirectory(); 36 sfd.RestoreDirectory = true; 37 sfd.Filter = "bmp files(*.bmp)|*.bmp|All Files(*.*)|*.*"; 38 sfd.FileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg"; 39 if (sfd.ShowDialog() == DialogResult.OK) 40 { 41 bitmap.Save(sfd.FileName, ImageFormat.Jpeg); 42 } 43 } 44 } 45 } 46 } 47 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现