Solved

Locating Bitmaps

Posted on 1998-08-03
8
304 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 143
Delphi: ForceDirectory plain function 7 31
Delphi inherited method 6 68
FMX and jaudiotracker playing memory stream 29 78
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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