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#實現截圖幫助類的文章就介紹到這瞭。希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: