網(wǎng)站首頁 編程語言 正文
幫助類實現(xiàn):
注意修改命名空間
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
class ScreenCapture
{
private Bitmap _screenshot = null;
/// <summary>
/// 截取全屏
/// </summary>
/// <returns>返回值</returns>
public Bitmap CaptureScreen()
{
return Capture(Rectangle.Empty, false);
}
/// <summary>
/// 截取全屏并保存
/// </summary>
/// <param name="fileName">文件路徑</param>
/// <returns></returns>
public Bitmap CaptureScreen(string fileName)
{
Bitmap screenshot = Capture(Rectangle.Empty, false);
saveImage(fileName, screenshot);
return screenshot;
}
/// <summary>
/// 截屏到剪切板
/// </summary>
public void CopyToClipboard()
{
if (this._screenshot != null)
Clipboard.SetImage(this._screenshot);
else if (this._screenshot == null)
MessageBox.Show("No screenshot found. Please take a screenshot first.", "Copy to Clipboard");
}
/// <summary>
/// 區(qū)域截屏
/// </summary>
/// <param name="rect">區(qū)域范圍</param>
/// <returns>返回值</returns>
public Bitmap CaptureRectangle(Rectangle rect)
{
return Capture(rect, true);
}
/// <summary>
/// 區(qū)域截屏
/// </summary>
/// <param name="rect">區(qū)域范圍</param>
/// <param name="fileName">文件路徑</param>
/// <returns>返回值</returns>
public Bitmap CaptureRectangle(Rectangle rect, string fileName)
{
Bitmap screenshot = Capture(rect, true);
saveImage(fileName, screenshot);
return screenshot;
}
private Bitmap Capture(Rectangle rect, bool isRect)
{
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
Bitmap screenshot = null;
if(!isRect)
screenshot = new Bitmap(screenWidth, screenHeight);
else if(isRect)
screenshot = new Bitmap(rect.Width, rect.Height);
Graphics g = Graphics.FromImage(screenshot);
if (!isRect)
{
g.CopyFromScreen(Point.Empty, Point.Empty, screenshot.Size);
}
else if (isRect)
{
g.CopyFromScreen(new Point(rect.X, rect.Y), Point.Empty, rect.Size);
}
this._screenshot = screenshot;
return screenshot;
}
private void saveImage(string fileName, Bitmap screenshot)
{
string ext = System.IO.Path.GetExtension(fileName);;
ext = ext.ToLower();
if (ext == ".jpg" || ext == ".jpeg")
screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
else if(ext == ".gif")
screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
else if(ext == ".png")
screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
else if (ext == ".bmp")
screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
else if (ext == ".tiff")
screenshot.Save(fileName, System.Drawing.Imaging.ImageFormat.Tiff);
}
}
}
原文鏈接:https://www.cnblogs.com/edielei/p/3432686.html
相關(guān)推薦
- 2022-12-10 Android入門之ScrollView的使用教程_Android
- 2024-03-16 nginx 報 unknown directive “server“ 詭異問題處理
- 2023-03-28 Python代碼庫之Tuple如何append添加元素問題_python
- 2022-12-07 C++?二維(多維)vector添加一個空項問題_C 語言
- 2022-01-09 ng-zorro中樹(nz-tree)的拖拽
- 2022-10-05 react-router-dom入門使用教程(前端路由原理)_React
- 2022-02-21 windows11安裝docker desktop
- 2022-09-14 C#實現(xiàn)自由組合本地緩存、分布式緩存和數(shù)據(jù)查詢_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支