question

Toduro-8190 avatar image
0 Votes"
Toduro-8190 asked Castorix31 answered

Mouse cursor not is drawn on screenshot

I have this following code that makes a screenshot of a region around mouse.

Now i tried insert the icon of mouse on screenshot, but nothing appear. How fix it?

 <StructLayout(LayoutKind.Sequential)>
     Public Structure CURSORINFO
         Public cbSize As Int32
         Public flags As Int32
         Public hCursor As IntPtr
         Public ptScreenPos As Point
     End Structure
    
     <StructLayout(LayoutKind.Sequential)>
     Public Structure ICONINFO
         Public fIcon As Boolean
         Public xHotspot As Int32
         Public yHotspot As Int32
         Public hbmMask As IntPtr
         Public hbmColor As IntPtr
     End Structure
    
     <DllImport("user32.dll", EntryPoint:="GetCursorInfo")>
     Public Shared Function GetCursorInfo(ByRef pci As CURSORINFO) As Boolean
     End Function
    
     <DllImport("user32.dll", EntryPoint:="CopyIcon")>
     Public Shared Function CopyIcon(ByVal hIcon As IntPtr) As IntPtr
     End Function
    
     <DllImport("user32.dll", EntryPoint:="GetIconInfo")>
     Public Shared Function GetIconInfo(ByVal hIcon As IntPtr, ByRef piconinfo As ICONINFO) As Boolean
     End Function
    
     Private Shared Function CaptureCursor(ByRef x As Integer, ByRef y As Integer) As Bitmap
         Dim bmp As Bitmap
         Dim hicon As IntPtr
         Dim ci As New CURSORINFO()
         Dim icInfo As ICONINFO
         ci.cbSize = Marshal.SizeOf(ci)
         If GetCursorInfo(ci) Then
             hicon = CopyIcon(ci.hCursor)
             If GetIconInfo(hicon, icInfo) Then
                 x = ci.ptScreenPos.X - CInt(icInfo.xHotspot)
                 y = ci.ptScreenPos.Y - CInt(icInfo.yHotspot)
                 Dim ic As Icon = Icon.FromHandle(hicon)
                 bmp = ic.ToBitmap()
                 ic.Dispose()
                 Return bmp
             End If
         End If
         Return Nothing
     End Function
    
     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
         Dim x As Integer
         Dim y As Integer
    
         Dim cursorBmp As Bitmap = CaptureCursor(x, y)
    
         Dim bmp As New Bitmap(Cursor.Size.Width, Cursor.Size.Height)
         Dim sourceLocation As Point = Control.MousePosition
    
         sourceLocation.Offset(-16, -16)
    
         Using g As Graphics = Graphics.FromImage(bmp)
             g.CopyFromScreen(sourceLocation, Point.Empty, bmp.Size)
             g.DrawImage(cursorBmp, x, y)
             'Cursor.Draw(g, New Rectangle(Cursor.Position, Cursor.Size))
             cursorBmp.Dispose()
         End Using
    
         Me.PictureBox1.Image = bmp
     End Sub



dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
1 Vote"
Castorix31 answered

You draw it outside the rectangle...

You can change :

 g.DrawImage(cursorBmp, x - sourceLocation.X, y - sourceLocation.Y)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.