Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6794887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:16:34+00:00 2026-05-26T18:16:34+00:00

I need to capture the framebuffer and save it into the file, i am

  • 0

I need to capture the framebuffer and save it into the file,
i am using son of grab example code to capture the buffer, but it doesn’t give me the mouse pointer,

So i am drawming the mouse pointer by my own, please refer the below code snippet, everything works fine, except cursor is not drawn at proper place, there is a slightly deviation in x & y coordinate, form the Cocoa framework, i am getting mouse pointer location, it seems somehow i should get mouse cursor bounds, so the same rect i can use to draw the cursor,
Any idea, how to use Mousse location pointer to draw the mouse image at correct location?

-(CGImageRef)appendMouseCursor:(CGImageRef)pSourceImage{
    // get the cursor image 
    NSPoint mouseLoc; 
    mouseLoc = [NSEvent mouseLocation]; //get cur

    NSLog(@"Mouse location is x=%d,y=%d",(int)mouseLoc.x,(int)mouseLoc.y);

    // get the mouse image 
    NSImage *overlay    =   [[[NSCursor arrowCursor] image] copy];

    NSLog(@"Mouse location is x=%d,y=%d cursor width = %d, cursor height = %d",(int)mouseLoc.x,(int)mouseLoc.y,(int)[overlay size].width,(int)[overlay size].height);

    int x = (int)mouseLoc.x;
    int y = (int)mouseLoc.y;
    int w = (int)[overlay size].width;
    int h = (int)[overlay size].height;
    int org_x = x-w/2;
    int org_y = y-h/2;

    size_t height = CGImageGetHeight(pSourceImage);
    size_t width =  CGImageGetWidth(pSourceImage);
    int bytesPerRow = CGImageGetBytesPerRow(pSourceImage);

    unsigned int * imgData = (unsigned int*)malloc(height*bytesPerRow);

    // have the graphics context now, 
    CGRect bgBoundingBox = CGRectMake (0, 0, width,height);

    CGContextRef context =  CGBitmapContextCreate(imgData, width, 
                                                  height, 
                                                  8, // 8 bits per component 
                                                  bytesPerRow, 
                                                  CGImageGetColorSpace(pSourceImage), 
                                                  CGImageGetBitmapInfo(pSourceImage));

    // first draw the image 
    CGContextDrawImage(context,bgBoundingBox,pSourceImage);

    // then mouse cursor 
    CGContextDrawImage(context,CGRectMake(0, 0, width,height),pSourceImage);

    // then mouse cursor 
    CGContextDrawImage(context,CGRectMake(org_x, org_y, w,h),[overlay CGImageForProposedRect: NULL context: NULL hints: NULL] );


    // assuming both the image has been drawn then create an Image Ref for that 

    CGImageRef pFinalImage = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    return pFinalImage; /* to be released by the caller */
}  

Everything works fine, except, there is slightly deviation in the mouse position, from the

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:16:35+00:00Added an answer on May 26, 2026 at 6:16 pm

    You need to take into account the mouse cursor’s hot spot, which is the pixel in the cursor that is the “active point”. You can get this from the -hotspot method of NSCursor which returns an NSPoint relative to the bottom-left corner of the cursor’s coordinate system.

    So your code should probably be something like:

    NSPoint offset = [[NSCursor arrowCursor] hotSpot];
    
    int org_x = (x - w/2) - offset.x;
    int org_y = (y - h/2) - offset.y;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to capture either \d+\.\d+ or \d+ but nothing else. For instance ,
I need to capture Oauth data coming from facebook into my table , i
I need to capture audio from a microphone using a flash object in a
I need to capture joystick input using C or Java (whichever is easier). There
I need to capture the stdout of a process I execute via subprocess into
My problem is I need to capture a keypress but at any given time
I need to capture an image from a webcam (tethered camera, etc.) into a
I need to capture a tab keypress event on some dynamic inputs, but the
I'll need to capture my bitstream into a string and keep concatenating the string.
I need to capture all links in a given html. Here is sample code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.