2011-07-19 10 views
12

मैं बिटमैप में स्क्रीन को कैप्चर करने के लिए नीचे दिए गए कोड का उपयोग कर रहा हूं। स्क्रीन पर कब्जा कर लिया गया है, लेकिन मैं स्क्रीन पर माउस पॉइंटर प्राप्त करने में असमर्थ हूं। क्या आप कुछ वैकल्पिक दृष्टिकोण सुझा सकते हैं ताकि माउस भी कब्जा कर लिया जा सके?विंडोज एपीआई का उपयोग कर स्क्रीन और माउस पॉइंटर कैप्चर कैसे करें?

private Bitmap CaptureScreen() 
    { 
     // Size size is how big an area to capture 
     // pointOrigin is the upper left corner of the area to capture 
     int width = Screen.PrimaryScreen.Bounds.X + Screen.PrimaryScreen.Bounds.Width; 
     int height = Screen.PrimaryScreen.Bounds.Y + Screen.PrimaryScreen.Bounds.Height; 
     Size size = new Size(width, height); 
     Point pointOfOrigin = new Point(0, 0); 

     Bitmap bitmap = new Bitmap(size.Width, size.Height); 
     { 
      using (Graphics graphics = Graphics.FromImage(bitmap)) 
      { 
       graphics.CopyFromScreen(pointOfOrigin, new Point(0, 0), size); 
      } 
      return bitmap; 
     } 
    } 

उत्तर

21
[StructLayout(LayoutKind.Sequential)] 
struct CURSORINFO 
{ 
    public Int32 cbSize; 
    public Int32 flags; 
    public IntPtr hCursor; 
    public POINTAPI ptScreenPos; 
} 

[StructLayout(LayoutKind.Sequential)] 
struct POINTAPI 
{ 
    public int x; 
    public int y; 
} 

[DllImport("user32.dll")] 
static extern bool GetCursorInfo(out CURSORINFO pci); 

[DllImport("user32.dll")] 
static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon); 

const Int32 CURSOR_SHOWING = 0x00000001; 

public static Bitmap CaptureScreen(bool CaptureMouse) 
{ 
    Bitmap result = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format24bppRgb); 

    try 
    { 
     using (Graphics g = Graphics.FromImage(result)) 
     { 
      g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); 

      if (CaptureMouse) 
      { 
       CURSORINFO pci; 
       pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO)); 

       if (GetCursorInfo(out pci)) 
       { 
        if (pci.flags == CURSOR_SHOWING) 
        { 
         DrawIcon(g.GetHdc(), pci.ptScreenPos.x, pci.ptScreenPos.y, pci.hCursor); 
         g.ReleaseHdc(); 
        } 
       } 
      } 
     } 
    } 
    catch 
    { 
     result = null; 
    } 

    return result; 
} 
+0

, अच्छा स्वच्छ और तेजी से कोड। धन्यवाद। –

+2

नोट, यह कोड कुछ कर्सर को गलत स्थिति में आकर्षित करेगा, क्योंकि आपको ऑफ़सेट के लिए 'GetIconInfo' कॉल करने की आवश्यकता है। इसके अलावा कुछ दिखाई देंगे, अधिक जानकारी के लिए: http://stackoverflow.com/questions/918990/ – WhoIsRich

संबंधित मुद्दे