2008-12-12 9 views
55

Je veux capturer l'écran dans mon code pour obtenir une image - comme utiliser le bouton «imprimer l'écran» sur le clavier.Capturer l'écran dans une image bitmap

Est-ce que quelqu'un a une idée de comment faire cela? Je n'ai pas de point de départ.

Répondre

95

Si vous utilisez le .NET Framework 2.0 (ou version ultérieure), vous pouvez utiliser la méthode CopyFromScreen() détaillée:

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

Nice et simple ... fonctionne comme un charme ... merci! –

+1

Votre réponse est incorrecte s'il vous plaît mettre à jour avec (g.CopyFromScreen ( 0, 0 , 0, 0 , Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); –

+1

Mais ça marche sur multi-écrans 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

Votre code ne sort pas une image correcte. Est-ce censé être un JPEG ou un PNG? –

+3

Fonctionne bien, mais il y a un gel général de l'interface utilisateur. Incompatible pour les projets (comme le mien) qui ont besoin d'analyser l'écran 10-20 fois par seconde. – Jurion

0

Essayez this Code

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); 
+0

Il est recommandé d'ajouter quelques explications à votre code pour que les personnes lisant votre réponse comprennent mieux et plus facilement. – Ibo

Questions connexes