Solved

Locating Bitmaps

Posted on 1998-08-03
8
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 100 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 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