2010-07-13 19 views
6

मैं बिटमैप ऑब्जेक्ट पर स्क्रीन कैप्चर करने के लिए Win32 PrintWindow फ़ंक्शन का उपयोग कर रहा हूं।स्क्रीन के हिस्से को कैप्चर करने के लिए

यदि मैं केवल खिड़की का एक क्षेत्र कैप्चर करना चाहता हूं, तो मैं छवि को स्मृति में कैसे फसल कर सकता हूं?

[System.Runtime.InteropServices.DllImport(strUSER32DLL, CharSet = CharSet.Auto, SetLastError = true)] 
public static extern int PrintWindow(IntPtr hWnd, IntPtr hBltDC, uint iFlags); 

public enum enPrintWindowFlags : uint 
{ 
    /// <summary> 
    /// 
    /// </summary> 
    PW_ALL = 0x00000000, 
    /// <summary> 
    /// Only the client area of the window is copied. By default, the entire window is copied. 
    /// </summary> 
    PW_CLIENTONLY = 0x00000001 
} 

public System.Drawing.Bitmap CaptureWindow(IntPtr hWnd, enPrintWindowFlags eFlags) 
{ 
    System.Drawing.Rectangle rctForm = System.Drawing.Rectangle.Empty; 

    using(System.Drawing.Graphics grfx = System.Drawing.Graphics.FromHdc(GetWindowDC(hWnd))) 
    { 
     rctForm = System.Drawing.Rectangle.Round(grfx.VisibleClipBounds); 
    } 

    System.Drawing.Bitmap pImage = new System.Drawing.Bitmap(rctForm.Width, rctForm.Height); 
    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(pImage); 

    IntPtr hDC = graphics.GetHdc();   
    //paint control onto graphics using provided options   
    try 
    {    
     PrintWindow(hWnd, hDC, (uint)eFlags);  
    } 
    finally 
    {    
     graphics.ReleaseHdc(hDC);   
    }  
    return pImage; 
} 

उत्तर

3

आप बस पूरे स्क्रीन खींचते हैं और फिर एक फसल समारोह है कि कुल छवि का एक क्षेत्र का चयन करता है में छवि गुजर सकता है:

यहाँ कोड मैं पूरी विंडो पर कब्जा करने का उपयोग कर रहा है। Bitmap.Clone() विधि पर एक नज़र डालें। जैसे

public Bitmap CropBitmap(Bitmap bitmap, int cropX, int cropY, int cropWidth, int cropHeight) 
{ 
Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight); 
Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat); 
return cropped; 
} 

ध्यान दें, मैं इस नीचे से this blog

+0

यही वह था जो मैं सोच रहा था ... वास्तव में केवल उस हिस्से को पकड़ना संभव हो सकता है लेकिन यह बहुत आसान है। – Adam

0

खुद को बचाने कुछ परेशानी खींच लिया और Cropper के स्रोत लेने।

3

स्क्रीन पर कब्जा करने और 100 पिक्सेल वर्ग की एक फसल छवि बनाने के लिए यहां पूरा कोड है। कोड एक बटन क्लिक घटना से लिया जाता है। आपको जो चाहिए वह प्रयोग करें।

Bitmap screenShot = null; 
     Bitmap croppedImage; 
     Graphics screen; 

     if(saveFileDialog.ShowDialog() == DialogResult.OK) 
     { 
      this.Hide(); 
      screenShot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
            Screen.PrimaryScreen.Bounds.Height, 
            PixelFormat.Format32bppArgb); 
      screen = Graphics.FromImage(screenShot); 
      screen.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, 
            Screen.PrimaryScreen.Bounds.Y, 
            0, 
            0, 
            Screen.PrimaryScreen.Bounds.Size, 
            CopyPixelOperation.SourceCopy); 
      screenShot.Save(saveFileDialog.FileName, ImageFormat.Png); 
      this.Show(); 
     } 

     //crop image 
     if(screenShot != null) 
     { 
      if(saveFileDialog.ShowDialog() == DialogResult.OK) 
      { 
       int x = 100; 
       int y = 100; 
       int xWidth = 100; 
       int yHeight = 100; 
       Rectangle rect = new Rectangle(x, y, xWidth, yHeight); 
       croppedImage = screenShot.Clone(rect, PixelFormat.Format32bppArgb); 
       if (croppedImage != null) 
       { 
        croppedImage.Save(saveFileDialog.FileName, ImageFormat.Png); 
       }  
      }     
     } 
संबंधित मुद्दे