November 20, 2011

Capture Screenshot

นี่คือเทคนิคอีกอย่างหนึ่ง สำหรับการเขียนโปรแกรม Capture Screenshot (หรือ Print Screen นั่นเอง) แล้วไปเก็บไว้ในแฟ้มหรือโฟลเดอร์ที่ต้องการ

ส่วนหัวของโปรแกรมที่จำเป็น :
using System.Drawing;
using System.Drawing.Imaging;
ตัวอย่างการเขียนโค้ด :
private void Screenshot()
{
   Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
   Graphics graphics = Graphics.FromImage(printscreen as Image);
   graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
   printscreen.Save(string path, ImageFormat.Jpeg);   
   //e.g. printscreen.Save(@"C:\peezg\screenshot.jpg", ImageFormat.Jpeg);
 }
ถ้าต้องการเซฟรูปภาพเป็นไฟล์ประเภทอื่นเช่น Bmp, Gif, Png, Tiff etc. ก็สามารถทำได้เหมือนกัน เพียงแก้ไขโค้ดบรรทัดสุดท้ายเท่านั้น
see more : http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html

No comments:

Post a Comment