今天小编给大家分享一下C#如何实现截图帮助类的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
帮助类实现:
注意修改命名空间
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> /// 区域截屏 /// </summary> /// <param name="rect">区域范围</param> /// <returns>返回值</returns> public Bitmap CaptureRectangle(Rectangle rect) { return Capture(rect, true); } /// <summary> /// 区域截屏 /// </summary> /// <param name="rect">区域范围</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); } } }
以上就是“C#如何实现截图帮助类”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。