2008-12-12 15 views
55

Klavyede 'print screen' düğmesini kullanmak gibi bir görüntü elde etmek için ekranımdaki kodu yakalamak istiyorum.Ekranı Bitmap'e Kaydetme

Bunun nasıl yapılacağı hakkında bir fikri olan var mı? Başlangıç ​​noktası yok. .NET 2.0 (veya üstü) çerçevesini kullanarak burada ayrıntılı CopyFromScreen() yöntemi kullanabilirsiniz Eğer

cevap

95

:

http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html

//Create a new bitmap. 
var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
           Screen.PrimaryScreen.Bounds.Height, 
           PixelFormat.Format32bppArgb); 

// Create a graphics object from the bitmap. 
var gfxScreenshot = Graphics.FromImage(bmpScreenshot); 

// Take the screenshot from the upper left corner to the right bottom corner. 
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, 
          Screen.PrimaryScreen.Bounds.Y, 
          0, 
          0, 
          Screen.PrimaryScreen.Bounds.Size, 
          CopyPixelOperation.SourceCopy); 

// Save the screenshot to the specified path that the user has chosen. 
bmpScreenshot.Save("Screenshot.png", ImageFormat.Png); 
+0

Güzel ve basit ... bir çekicilik gibi çalışır ... teşekkürler! –

+1

Yanıtınız (g.CopyFromScreen (ile güncelleyin yanlıştır 0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); –

+1

Ama çok monitörde çalışır pc – EaterOfCode

5
// Use this version to capture the full extended desktop (i.e. multiple screens) 

Bitmap screenshot = new Bitmap(SystemInformation.VirtualScreen.Width, 
           SystemInformation.VirtualScreen.Height, 
           PixelFormat.Format32bppArgb); 
Graphics screenGraph = Graphics.FromImage(screenshot); 
screenGraph.CopyFromScreen(SystemInformation.VirtualScreen.X, 
          SystemInformation.VirtualScreen.Y, 
          0, 
          0, 
          SystemInformation.VirtualScreen.Size, 
          CopyPixelOperation.SourceCopy); 

screenshot.Save("Screenshot.png", System.Drawing.Imaging.ImageFormat.Png); 
+0

Kodunuz düzgün bir görüntü oluşturmuyor. JPEG mi, PNG mi olmalı? –

+3

İyi çalışıyor, ancak genel bir UI donması var. Ekranı 10-20 kez saniyede analiz etmeniz gereken projeler (benimki gibi) için uyumsuz. – Jurion

0

this kodu

Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); 
Graphics gr = Graphics.FromImage(bmp); 
gr.CopyFromScreen(0, 0, 0, 0, bmp.Size); 
pictureBox1.Image = bmp; 
bmp.Save("img.png",System.Drawing.Imaging.ImageFormat.Png); 
0
Bitmap memoryImage; 
//Set full width, height for image 
memoryImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
         Screen.PrimaryScreen.Bounds.Height, 
         PixelFormat.Format32bppArgb); 
Size s = new Size(memoryImage.Width, memoryImage.Height); 
Graphics memoryGraphics = Graphics.FromImage(memoryImage); 
memoryGraphics.CopyFromScreen(0, 0, 0, 0, s); 
string str = ""; 
try 
{ 
    str = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + 
      @"\Screenshot.png");//Set folder to save image 
} 
catch { }; 
memoryImage.save(str); 
deneyin
+0

Kodunuza bazı açıklamalar eklemeniz önerilir, böylece cevabınızı okuyan insanlar daha iyi ve daha kolay anlarlar. – Ibo