CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Capture Image Using App Window as 0,0 (x,y) Reference

    I am trying to grab an image of a particluar rectangular area of a window that is not part of my code (an external app). I have the handle of the window, that part is easy enough. My stuggle is that I can't seem to find code that will allow me to target a window and then take a rectangular area of that window and save it to an image.

    CopyFromScreen is just that, the entire screen. This particular app window can not be sized but i can be moved. So as long as I could make the top left corner of this app window be my 0,0 (x,y) reference I could always be sure I'm getting the correct area of the screen.

    I am fairly new to C# so part of the issue may be that I don't know the "buzz" words for searching for help on these forums or Google. So any help would be appreciated. I am using Visual Studio 2008.

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Capture Image Using App Window as 0,0 (x,y) Reference

    Most programs I know for example for scanning reasons or capturing a part of the screen simple allow the user to drag a rectangle as a dotted line around what he wants to scan. This way you can simple get that part of the full screen capture which you want to have. Coordinates and size of the rectangle which surrounds the wanted picture are known so the problem is reduced to calculate the offset to the full screen picture
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    Apr 2009
    Posts
    4

    Re: Capture Image Using App Window as 0,0 (x,y) Reference

    Quote Originally Posted by JonnyPoet View Post
    Most programs I know for example for scanning reasons or capturing a part of the screen simple allow the user to drag a rectangle as a dotted line around what he wants to scan. This way you can simple get that part of the full screen capture which you want to have. Coordinates and size of the rectangle which surrounds the wanted picture are known so the problem is reduced to calculate the offset to the full screen picture
    Thank you for the reply. I have seen some information in regards to your suggestion however, this does require manual input from the user. I would like this to be automated. As I stated in my first post I can do something like this

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
             
                
    
                // Copy the defined area to clipboard
                // (start point x, start point y, width x, width y)
                Rectangle region = new Rectangle(0, 0, 50, 50);
                using (Bitmap bitmap = new Bitmap(region.Width, region.Height,
                    System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                using (Graphics bitmapGraphics = Graphics.FromImage(bitmap))
                {
                    bitmapGraphics.CopyFromScreen(region.Left, region.Top, 0, 0, region.Size);
                    Clipboard.SetImage(bitmap);
                }
    
    
            }
    This will allow me to get a 50 by 50 region of the screen, which is perfect. The only issue I have is that this is from the entire screen. I would like to identify my target windows handle (which I can do) and use it's corner as the 0,0 reference. This way I can create my offset and grab the 50 by 50 image I want, that will always be in the same place. Furthermore, this can be done with no interaction from the user.

    Alternately, if I had a way to determine what the coordinates for the target window where I could use them as an offset. Can anyone tell me how to get that information?

    Any additional help would be very much apprecieated.

    Thank you.
    Last edited by JoorStard; April 30th, 2009 at 01:50 PM. Reason: Alternate idea to solve problem

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Capture Image Using App Window as 0,0 (x,y) Reference

    Quote Originally Posted by JoorStard View Post
    Alternately, if I had a way to determine what the coordinates for the target window where I could use them as an offset. Can anyone tell me how to get that information?

    Any additional help would be very much apprecieated.

    Thank you.
    AS nobody knows in which way the picture you want to grab is drawn you will have problems to get the coordinates where it begins. User input in such cases IMHO is necessary There is still no substitute for all of our human intelligence.
    There may be ways to find the picture if it has clearly defined vertical and horizontal borders. Then you maybe can scan for colorjumps along a line. But all this IMHO is very very complicated and only done if there is an absolute need to do it like in OCR programs for textscanning This is done by absolute experts and I dont know anything about their patents they may have on such code
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Apr 2009
    Posts
    4

    Re: Capture Image Using App Window as 0,0 (x,y) Reference

    I actually have the OCR piece written. On a test form I load the target image that I have created from print screen key + MS Paint and it performs the desired functions. However, I need the imput to not be manually loaded or selected by the user.

    After some more research I think all I really need is a way to determine the location (coordinates) of this external application window. For example; if I knew that the top left corner was at 166x by 254y then I could use those numbers as my offset for a CopyFromScreen. Alternately I could find the application window's distance from the left of the screen and top of the screen to populate the offset.

    If I provide the application windows handle can I get the location of the window via an API call?

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Capture Image Using App Window as 0,0 (x,y) Reference

    Quote Originally Posted by JoorStard View Post
    I actually have the OCR piece written. On a test form I load the target image that I have created from print screen key + MS Paint and it performs the desired functions. However, I need the imput to not be manually loaded or selected by the user.
    What do you want toperform here. ??? This sounds me abit strange. Is this an automated Picture reader ? which is able to read text of a picture ? Something similar ? What do you want to perform with this task ? Or maybe its easier to answer your question by showing a picture of what you need to find on the screen. Maybe do a screenshot and show an example, or describe wahts inside the picture, so I can get an idea of that task you need to do.
    Last edited by JonnyPoet; May 1st, 2009 at 08:57 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured