1.當前窗體
public static Image CaptureControl(Control ctrl){System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ctrl.Width, ctrl.Height);ctrl.DrawToBitmap(bmp, new Rectangle(0, 0, ctrl.Width, ctrl.Height));return bmp;}private void DownLoad(){string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取應用程序運行的路徑string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";//以日期命名文件名try{var image = CaptureControl(this);image.Save(filePath + fileName, ImageFormat.Png);}catch (Exception ex){//異常處理 }}
2.當前屏幕
private void saveB(){string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取應用程序運行的路徑string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";//以日期命名文件名try{Screen screen = Screen.AllScreens.FirstOrDefault();//獲取當前第一塊屏幕(根據需求也可以換其他屏幕)//創建需要截取的屏幕區域 Rectangle rc = new Rectangle(0, 0, 200, 200);//生成截圖的位圖容器 System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);//GDI+圖像畫布 using (Graphics memoryGrahics = Graphics.FromImage(bitmap)){memoryGrahics.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy);//對屏幕指定區域進行圖像復制}bitmap.Save(filePath + fileName, ImageFormat.Png);MessageBox.Show("文件保存在:" + filePath + fileName);}catch (Exception ex){//異常處理MessageBox.Show(ex.ToString());}}
3.?當前屏幕
?
private void CaptureScreen(){// 創建一個和屏幕一樣大小的BitmapRectangle bounds = Screen.PrimaryScreen.Bounds;using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(bounds.Width, bounds.Height)){// 拷貝屏幕到Bitmapusing (Graphics g = Graphics.FromImage(bitmap)){g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);}// 保存Bitmap到文件string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "screenshot.png";bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);}}