Winform 根据Point截图并保存到指定路径
1 /// <summary> 2 /// 获取图片流 3 /// </summary> 4 /// <param name="ImageXY">图片屏幕起始点</param> 5 /// <param name="ImageSize">图片大小</param> 6 /// <returns></returns> 7 public string CutImage(Point ImageXY, Size ImageSize, string FilePath,string FileName) 8 { 9 int[] sCreem = { Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height }; 10 Bitmap bmp = new Bitmap(sCreem[0], sCreem[1]); 11 Graphics g = Graphics.FromImage(bmp); 12 g.CopyFromScreen(0, 0, 0, 0, new Size(sCreem[0], sCreem[1])); 13 string FileNamePath = ""; 14 try 15 { 16 Rectangle rect = new Rectangle(ImageXY, ImageSize); 17 if (!rect.IsEmpty) 18 { 19 Bitmap imgbmp = new Bitmap(rect.Width, rect.Height); 20 imgbmp = bmp.Clone(rect, PixelFormat.Format32bppRgb); 21 FileNamePath = ImageSave(imgbmp, FilePath, FileName); 22 } 23 return FileNamePath; 24 } 25 catch (Exception ex) 26 { 27 throw ex; 28 } 29 } 30 /// <summary> 31 /// 保存截图 32 /// </summary> 33 /// <param name="bmp"></param> 34 public string ImageSave(Bitmap bmp,string FilePath,string FileName) 35 { 36 try 37 { 38 SaveFileDialog save = new SaveFileDialog(); 39 if (!Directory.Exists(FilePath))//如果不存在就创建file文件夹 40 { 41 Directory.CreateDirectory(FilePath);//创建该文件夹 42 } 43 save.FileName = FilePath; 44 bmp.Save(save.FileName + FileName + ".jpg", ImageFormat.Jpeg); 45 return FilePath + FileName + ".jpg"; 46 } 47 catch (Exception ex) 48 { 49 throw ex; 50 } 51 }
1 cut.CutImage(PointToScreen(Control.Location), Control.Size, System.IO.Directory.GetCurrentDirectory() + "\\Image\\", FileName);