Solved

Locating Bitmaps

Posted on 1998-08-03
8
294 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now