Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Locating Bitmaps

Posted on 1998-08-03
8
Medium Priority
?
329 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
Independent Software Vendors: 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 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

719 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