Note that there are some explanatory texts on larger screens.

plurals
  1. POC# - DrawIcon not using cursor mask when drawing to image (Example for I-beam cursor)
    primarykey
    data
    text
    <p>If i use DrawIcon when taking a screenshot, it can draw I-beam cursor with mask properly but if I want to use DrawIcon on any image then masking does not work.</p> <p>This DrawIcon example works properly:</p> <pre><code> public static Image CaptureRectangleNative(IntPtr handle, Rectangle rect, bool captureCursor = false) { if (rect.Width == 0 || rect.Height == 0) { return null; } IntPtr hdcSrc = NativeMethods.GetWindowDC(handle); IntPtr hdcDest = NativeMethods.CreateCompatibleDC(hdcSrc); IntPtr hBitmap = NativeMethods.CreateCompatibleBitmap(hdcSrc, rect.Width, rect.Height); IntPtr hOld = NativeMethods.SelectObject(hdcDest, hBitmap); NativeMethods.BitBlt(hdcDest, 0, 0, rect.Width, rect.Height, hdcSrc, rect.X, rect.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); if (captureCursor) { Point cursorOffset = CaptureHelpers.ScreenToClient(rect.Location); try { using (CursorData cursorData = new CursorData()) { cursorData.DrawCursorToHandle(hdcDest, cursorOffset); } } catch (Exception e) { DebugHelper.WriteException(e, "Cursor capture failed."); } } NativeMethods.SelectObject(hdcDest, hOld); NativeMethods.DeleteDC(hdcDest); NativeMethods.ReleaseDC(handle, hdcSrc); Image img = Image.FromHbitmap(hBitmap); NativeMethods.DeleteObject(hBitmap); return img; } // This function inside CursorData class public void DrawCursorToHandle(IntPtr hdcDest, Point cursorOffset) { if (IconHandle != IntPtr.Zero) { Point drawPosition = new Point(Position.X - cursorOffset.X, Position.Y - cursorOffset.Y); NativeMethods.DrawIcon(hdcDest, drawPosition.X, drawPosition.Y, IconHandle); } } </code></pre> <p>But this DrawIcon example is not using a mask, so I-beam icon becomes white and in a white background it's not visible:</p> <pre><code> public void DrawCursorToImage(Image img, Point cursorOffset) { if (IconHandle != IntPtr.Zero) { Point drawPosition = new Point(Position.X - cursorOffset.X, Position.Y - cursorOffset.Y); using (Graphics g = Graphics.FromImage(img)) { g.DrawIcon(Icon.FromHandle(IconHandle), drawPosition.X, drawPosition.Y); } } } </code></pre> <p>I think if I don't use desktop hdc then DrawIcon is not using a mask. How can I fix DrawCursorToImage method so it can use cursor mask properly? By the way, i read this post: <a href="https://stackoverflow.com/questions/918990/c-sharp-capturing-the-mouse-cursor-image">C# - Capturing the Mouse cursor image</a> and it don't have a solution to my problem too. Thanks for reading this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload