Thread: Taking a screenshot of an application.

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    62

    Taking a screenshot of an application.

    Ok, my end goal is to be able to read in the colors of every pixel in a window and process them. I know how to do this with windows, but have little idea where to get started with windows.

    With windows its basically a simple process of calling "GetDIBSection" and "BitBlt" and viola, you have an array with the data for every pixel on a given window.

    With X, however, the process is much more vague.

    I've read up what I could, and guess that you can do something to the effect of calling XGetWindowAttributes and then accessing the pixel data directly through the Value* value; part of the attributes structure. However, I am unsure if that will even work (don't have access to linux right now).

    Anyone help from someone with experience programming X would be much appreciated.

  2. #2

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Ok, so it is basically a call to XGetWindowsAttributes to get the data about the window. and then XGetImage to get the pixel data from the window. After a little googling, it looks like XGetPixel will give me the pixel values for each pixel on the image. Is this the fastest way to get pixel values or is there a faster on? (Speed is somewhat important)

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You could access and interpret XImage.data directly I suppose.

    gg

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Cogman View Post
    Ok, so it is basically a call to XGetWindowsAttributes to get the data about the window. and then XGetImage to get the pixel data from the window. After a little googling, it looks like XGetPixel will give me the pixel values for each pixel on the image. Is this the fastest way to get pixel values or is there a faster on? (Speed is somewhat important)
    You access the image data directly through the XImage->data field. Make sure to respect the metrics when accessing the data (i.e. byte_order, bytes_per_line, etc)

    An alternative is to create the image yourself with XCreateImage() then use XCopyArea() to copy pixels from the window into the image.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Ok, Thanks for all the help thus far, I have one last question. I couldn't find this on any of the documentation.

    I assume that XCopyArea dynamically allocates memory (and therefore needs to be freed). Does XGetImage dynamically allocate memory as well? Do I need to delete it to avoid memory leaks?

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Cogman View Post
    Ok, Thanks for all the help thus far, I have one last question. I couldn't find this on any of the documentation.

    I assume that XCopyArea dynamically allocates memory (and therefore needs to be freed). Does XGetImage dynamically allocate memory as well? Do I need to delete it to avoid memory leaks?
    XDestroyImage() should clean up the image and the raster buffer. For images you have created yourself, XDestroyImage() will deallocate the raster buffer for you. XCopyArea() has nothing to do with allocation, it just copies pixels from one drawable to another.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    All right then, that makes a lot of sense, Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. taking screenshot
    By qwertylol2 in forum C++ Programming
    Replies: 2
    Last Post: 05-06-2007, 12:26 AM
  2. Taking Screenshot - Full Screen Dos
    By loko in forum C Programming
    Replies: 12
    Last Post: 07-16-2005, 01:23 AM
  3. Taking a screenshot
    By Jareds411 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2004, 05:11 AM
  4. Taking a screenshot in DirectDraw
    By Stan100 in forum Game Programming
    Replies: 3
    Last Post: 11-13-2003, 11:26 PM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM