標(biāo)題: C#實現(xiàn)全屏截圖 [打印本頁]

作者: hongniu    時間: 2015-6-25 16:50
標(biāo)題: C#實現(xiàn)全屏截圖
C#實現(xiàn)全屏截圖,雖然沒有什么大用吧、但還是記錄下吧!

    直接代碼、沒什么好解釋的:

    
using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Threading;

    namespace Screenshot
    {
        class Program
        {
            public static int width = 0;   // 截圖的寬
            public static int height = 0;  // 截圖的高

            static void Main(string[] args)
            {
                Console.WriteLine("3秒后開始全屏截圖!");
                Thread.Sleep(3000); // 當(dāng)前線程休眠3S
                width = Screen.PrimaryScreen.Bounds.Width;  // 獲取系統(tǒng)寬
                height = Screen.PrimaryScreen.Bounds.Height;    // 獲取系統(tǒng)高
                string strFile = "C:\\" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".jpg";    // 文件保存的路徑
                ScreenShow(strFile, width, height); 
                Console.WriteLine("提示:截圖保存為" + strFile);
                Console.ReadKey();  // 等待用戶輸入退出
            }


            /// <summary>
            /// 全屏截圖
            /// </summary>
            /// <param name="strFileName">保存路徑</param>
            /// <param name="nWidth">圖片寬</param>
            /// <param name="nHeight">圖片高</param>
            /// <returns></returns>
            public static Bitmap ScreenShow(string strFileName, int nWidth, int nHeight)
            {
                Bitmap btm = new Bitmap(nWidth, nHeight);   // 新建一個Bitmap對象
                using (Graphics g = Graphics.FromImage(btm))
                {
                   g.CopyFromScreen(0, 0, 0, 0, Screen.AllScreens[0].Bounds.Size); // 獲取第0個顯示器的大小
                    g.Dispose();
                    btm.Save(strFileName, ImageFormat.Jpeg);    // 保存
                }
                return btm;
            }
        }
    }




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1