×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to determine if Form is obscured.

How to determine if Form is obscured.

How to determine if Form is obscured.

(OP)
Hi
I am developing an application in C# .NET which needs to create a bitmap image of a Windows Form, save it to disk and then do some OCR recognition on parts of that screen shot.  This is fine and I can do it.
If the window is obscured by another one, then my screen shot may be invalid for the OCR recognition process.
I need to find a way therefore, to determine if some of the window is obscured by another.
I know the Window Handle (IntPtr), its location on the desktop (x,y) and the window size (as RECT)
Any help or inspiration would be gratefully received.

regards

RogerDodge

RE: How to determine if Form is obscured.

Is the Windows Form a form you've created in C#?

If so how are you creating the bitmap of the form (and I assume, it's controls)?

HarleyQuinn
---------------------------------
Black coat, white shoes, black hat, cadillac. The boy's a timebomb!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers: How to get the best answers before post

RE: How to determine if Form is obscured.

>needs to create a bitmap image of a Windows Form

How are you doing that?

RE: How to determine if Form is obscured.

(OP)
The form which I need to Capture is from another application, I have no control over it programmatically.

Essentially, I am trying to "scrape" information from the form to then do some look-ups in a database from predetermined areas (fields) on the form.

I have set up some code within a "dummy" form, so when I click a button, I call this code which then gets the handle of itself and then captures the screen from its position, this works fine.


CODE


[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

private void cmdOptions_Click(object sender, EventArgs e)
{
RECT rct;
HandleRef lcHnd = new HandleRef(this, this.Handle);
if (!GetWindowRect((IntPtr)lcHnd, out rct))
{
    MessageBox.Show("ERROR");
}
else
{
    int windowWidth = System.Math.Abs(rct.Right - rct.Left);
    int windowHeight = System.Math.Abs(rct.Bottom - rct.Top);
    System.Drawing.Bitmap currBMP = new Bitmap(windowWidth, windowHeight);
    currBMP = CaptureClip(rct.Left,rct.Top, windowWidth, windowHeight);
                currBMP.Save("C:\\OCRSDK\\CaptBMP.bmp");
}
}

The CaptureClip function just "grabs" the defined area from the screen.

I have been exploring the possibilities of using "WindowFromPoint" and "PtInRect" API functions to try to figure something out.

RogerDodge

RE: How to determine if Form is obscured.

You might want to use the PrintWindow function which captures the window easily and works even if the window is obscured by other windows.

RE: How to determine if Form is obscured.

>The CaptureClip function just "grabs" the defined area from the screen

It is how the grabbing is being done that I was asking about. Unfortunately what you have posted show this. And I am not familiar with the CaptureClip function. Is this something that you wrote? And if so, can we see it please?

RE: How to determine if Form is obscured.

(OP)
I'll try the PrintWindow tomorrow when I'm back @ work - thanks.

strongm - here's the code (for clarity's sake)
(thanks to Danny Battison)

CODE

public static System.Drawing.Bitmap CaptureClip(int x, int y, int width, int height)
{
    System.Drawing.Bitmap BMP = new Bitmap(width, height,
                                           System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    System.Drawing.Graphics GFX = System.Drawing.Graphics.FromImage(BMP);
    GFX.CopyFromScreen (x, y, 0, 0, new Size(width, height),
                        System.Drawing.CopyPixelOperation.SourceCopy);
    
    return BMP;            
}

I'm sure I can tidy this code up a bit, but for the moment I just need the principles to work.

My main issue at the moment is to capture the hidden window (normally it wont be hidden but I cant be certain so I need to either trap it or not care - hopefully the PrintWindow will solve it.



 

RE: How to determine if Form is obscured.

(OP)
It doesn't specifically?  Should it?

RE: How to determine if Form is obscured.

(OP)
Mike - I think I may have misunderstood your question.  Being more awake! and looking at the links under your post, it may be that we are looking at the same underlying issues.
The application(s) which I am trying to "screen scrape" have all (as far as I can make out) been developed with Foxpro9. As a consequence, it seems that I am unable to enumerate any objects on the child forms.  In some applications, the relevant information which I am trying to obtain, ie client reference number, is present on the main window title text - I can then just use this information to do a lookup of the appropriate database record.
I will be interested to hear from you.

 

RE: How to determine if Form is obscured.

I should try to solve this problem on two ways:

Because your CaptureClip-function use the graphic handle of the console-screen (I actually more guess it then I known) I should make/force the window as Top-Level-Window during the capture period.

Or:

I would try to give a repaint-command of the window that you like to capture, but to the canvas of an bitmap instead of those of the Form.

 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close