Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Locating Bitmaps

Posted on 1998-08-03
8
Medium Priority
?
334 Views
Last Modified: 2010-04-04
Hi ppl,

Does anyone know how to locate a bitmap on the screen that is not part of your program - ie reading pixels from absolute coordinates on the screen. For example, in wordpad, finding the coordinates of the top left hand corner of the "save" icon by reading pixels and matching it with a "save" icon bitmap in memory, if you get what i mean. Or if there is a better way than that which works generally (not wordpad specific) i'd love to know. Wordpad is just an example btw, that's not the exact implementation of what i want to do :)

I'm using Delphi 2 btw.
0
Comment
Question by:lisa_g
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Author Comment

by:lisa_g
ID: 1359634
Edited text of question
0
 
LVL 5

Expert Comment

by:inter
ID: 1359635
Hi,
I think you try to solve the general problems of patern recognition :-) It is FEASIBLE only if the image you have is approximatelly equivalent to the image on the screen. The easiest way is as follows:

1 - Get the screen DC
2 - Starting from the top left corner of the screen BY USING MEAN SQUARE ERROR criterion and using IMAGE DIFFERENCE examine all the screen and choose the position giving the minimum difference.

This is time consuming but not problematic...
Regards, (ANY WAY WHY?)
Igor
0
 
LVL 1

Expert Comment

by:semuel
ID: 1359636
Hello.

I don't know about Delphi2, but if you want to direct access to the screen, you have a global varibe named "Screen", declard in the "Form.pas" unit, at least in Delphi3.

If you want to access other application, you'll have to mass with API calls. good luck.

Semuel.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:lisa_g
ID: 1359637
Well, i'll have an exact copy of the bitmap in memory, ie it matches what is on the screen perfectly - i just want to know where it is... so how do i read pixels off the screen? Nasty stuff is it? :(
0
 
LVL 3

Accepted Solution

by:
Matvey earned 400 total points
ID: 1359638
I’ll just write the "code behind Igor’s words". (Hope it’s not copyrighted, friend...)

You get the screen image (puts it into Image1):

Var
  DeskTopDC: HDc;
begin
  DeskTopDC := GetWindowDC(GetDeskTopWindow);
  with Image1.Picture.Bitmap do begin
    Width := Screen.Width;
    Height := Screen.Height;
    BitBlt(Canvas.Handle, 0,0, Width,  Height, DesktopDC, 1, 1, SrcCopy);
  end;
end;

Here’s a function that will find a sub-bitmap in a bitmap. It’ll find only exact matches:

function FindBitmap(subbmp, bmp: TBitmap): TPoint;
var
  x, y, x1, y1: Integer;
  pmatch: boolean;
begin
  result.x := -1;
  result.y := -1;
  for x := 0 to bmp.width-subbmp.width-1 do
   for y := 0 to bmp.height-subbmp.height-1 do
    if bmp.Canvas.Pixels[x, y]=subbmp.Canvas.Pixels[0, 0] then begin
     pmatch := true;
     for x1 := 0 to subbmp.width-1 do begin
      for y1 := 0 to subbmp.height-1 do
       if not(bmp.Canvas.Pixels[x+x1, y+y1]=subbmp.Canvas.Pixels[x1, y1]) then begin
        pmatch := false;
        break;
        end;
       if not pmatch then break
      end;
     if pmatch then begin
       result.x := x;
       result.y := y;
       exit;
     end;
    end;
end;

To test this do something like:

bmp1 := TBitmap.Create;
bmp1.width := 50;
bmp1.Height := 50;
bmp1.canvas.CopyRect(Rect(0, 0, 50, 50), Image1.Picture.Bitmap.Canvas, Rect(200, 200, 250, 250));
p := findbitmap(bmp1, Image1.Picture.Bitmap);
showMessage(inttostr(p.x)+#13+inttostr(p.y));

This should do it.
c u, bosism@netvision.net.il
0
 

Author Comment

by:lisa_g
ID: 1359639
Thanks a lot...

Should be handy :)

(have you tested the code?)
0
 
LVL 5

Expert Comment

by:inter
ID: 1359640
Hi there,
The code Matvey provides is good for exact match. (Copyright? how funny thanks...)
For inexact match we should compute each pixel difference and sum sqr's of all then
sqrt the result. By choosing the (x,y) that gives minimum diference sum we do inexact match which is known in literature as Optimum Match in Mean Square Distance Sense.
(For your information only)
Regards,
Igor
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1359641
Hi Igor, you're right of course - I also said it will find only exactly matching image parts.

Lisa, I tested it and it worked OK, though a bit slow.

c u
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question