2010-12-31 1 views
0

Je souhaite prendre une capture d'écran d'une section de mon écran, puis stocker cette information dans un tableau d'images. Y at-il un moyen de modifier cette classe: http://pastebin.com/PDPPxmPT afin qu'il me permette de prendre une capture d'écran d'une zone spécifique? Par exemple, si je voulais que les origines x, y des pixels de l'image bitmap soient de 200, 200 et que les destinations x, y soient de 600, 700, comment ferais-je cela?Prendre une capture d'écran d'une certaine section de l'écran en C#?

+0

Veuillez poster votre code dans votre question, et non sur Pastebin. –

Répondre

2

I mod il here

EDIT: Voici le code

public static Color[,] takeScreenshot(int x=0, int y=0, int width=0, int height = 0) 
     { 
    if (width==0) 
     width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; 
    if (height==0) 
     height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; 
      Bitmap screenShotBMP = new Bitmap(width, 
       height, PixelFormat.Format32bppArgb); 

      Graphics screenShotGraphics = Graphics.FromImage(screenShotBMP); 

      screenShotGraphics.CopyFromScreen(x, 
       y, 0, 0, new Size(width,height), 
       CopyPixelOperation.SourceCopy); 

      screenShotGraphics.Dispose(); 

      return bitmap2imagearray(screenShotBMP); 
     } 

public static Color[,] bitmap2imagearray(Bitmap b) 
     { 
      Color[,] imgArray = new Color[b.Width, b.Height]; 
      for (int y = 0; y < b.Height; y++) 
      { 
       for (int x = 0; x < b.Width; x++) 
       { 
        imgArray[x, y] = b.GetPixel(x, y); 
       } 
      } 
      return imgArray; 
     } 
+0

S'il vous plaît juste poster votre code dans votre réponse, et non sur Pastebin. –

0

Pas une réponse, mais y compris le code de pastebin car il disparaîtra probablement dans le futur et peut être utile pour les autres .

public static Color[,] takeScreenshot() 
    { 
     Bitmap screenShotBMP = new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, 
      System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb); 

     Graphics screenShotGraphics = Graphics.FromImage(screenShotBMP); 

     screenShotGraphics.CopyFromScreen(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X, 
      System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y, 0, 0, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size, 
      CopyPixelOperation.SourceCopy); 

     screenShotGraphics.Dispose(); 

     return bitmap2imagearray(screenShotBMP); 
    } 

    public static Color[,] bitmap2imagearray(Bitmap b) 
    { 
     Color[,] imgArray = new Color[b.Width, b.Height]; 
     for (int y = 0; y < b.Height; y++) 
     { 
      for (int x = 0; x < b.Width; x++) 
      { 
       imgArray[x, y] = b.GetPixel(x, y); 
      } 
     } 
     return imgArray; 
    } 
Questions connexes