C#實現擷取當前螢幕的方法
阿新 • • 發佈:2019-02-01
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Windows; using System.IO; using System.Windows.Forms; namespace CScreen { public class PrintUserScreen { /// <summary> /// C#擷取螢幕並儲存為圖片 /// </summary> public Image getScreen() { Image myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics g = Graphics.FromImage(myImage); g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)); IntPtr dc1 = g.GetHdc(); g.ReleaseHdc(dc1); //String path = "d:\\image\\"; // if (!Directory.Exists(path)) Directory.CreateDirectory(path); Random objRand = new Random(); String pic_name = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg"; //string allpathname = path + pic_name; //myImage.Save(allpathname); return myImage; } //將圖片轉換為二進位制流的方法 public byte[] ScreenStream(Bitmap bm) { System.IO.MemoryStream s = new System.IO.MemoryStream(); bm.Save(s, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] Data = s.ToArray(); s.Close(); s.Dispose(); return Data; } } }