?
Solved

How to get loaded image coordinates. Windows c++

Posted on 2012-08-22
13
Medium Priority
?
796 Views
Last Modified: 2012-08-23
I am using Visual Studio 2010 and have a c++ application where I have a main window dialog and within this dialog I display an image by calling LoadImage and several other calls to get the image loaded. Now later on in my program I what to get the current coordinates of this displayed image. I need some example code on how to accomplish this, i.e. x,y coordinates.
NO .NET EXAMPLES!

Thanks
0
Comment
Question by:atomicgs12
  • 6
  • 5
  • 2
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 38321426
How are you displaying that image? Assuming that you know the coordinates of that image within your dialog, you could use 'ClientToScreen()' (http://msdn.microsoft.com/en-us/library/windows/desktop/dd183434%28v=VS.85%29.aspx) to get the screen coordinates of your image, e.g.

POINT pt = { ...}; // coordinates of your image inside the dialog

ClientToScreen(hDlg, &pt);

Open in new window

0
 

Author Comment

by:atomicgs12
ID: 38321446
From my call I know nothing of the image, I do not have a handle to the dialog, hDlg, I need to get this from the displayed window. As I stated I have a displayed image in which i load an image, ex. .jpg, .bmp, ... At the time of my call where I need to know the coordinates of the loaded image I have no other information than then the handle to the main displayed window. There will always only be one image within the main display window. So I need to know how to get the handle to the image displayed within the main widow.
0
 

Author Comment

by:atomicgs12
ID: 38321456
Correction: sentence from above "As I stated I have a displayed image in which i load an image, ex. .jpg, .bmp, ..." should read "As I stated I have a displayed window in which i load an image, ex. .jpg, .bmp, ..."
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 86

Expert Comment

by:jkr
ID: 38321517
Thus my question: How are you displaying the image?
0
 

Author Comment

by:atomicgs12
ID: 38321832
Again, as I stated in my original post 'LoadImage()'. And besides "How are you displaying the image?" is a pretty generic question. Be more specific.
0
 
LVL 86

Expert Comment

by:jkr
ID: 38322164
OK, 'LoadImage()' does what the name says - it gives you a handle to an image, nothing more. Maybe some code could shed in some light. What are you doing with that image handle after this call?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38322180
LOL...this is an entertaining thread to follow.  The image gets displayed by ~magic~ jkr!  ;)
0
 

Author Comment

by:atomicgs12
ID: 38325704
CBitmap bmp;
   if (bmp=phBitmap = (HBITMAP)LoadImage(NULL, sPathToImage, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE)))
   {
      // Get the size of the bitmap
      BITMAP bmpInfo;
      bmp.GetBitmap(&bmpInfo);

      // Create an in-memory DC compatible with the
      // display DC we're using to paint
      CDC dcMemory;
      dcMemory.CreateCompatibleDC(pDC);

      // Select the bitmap into the in-memory DC
      CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);

      // Find a centerpoint for the bitmap in the client area
      CRect rect;
      GetClientRect(&rect);
      int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
      int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;

      // Copy the bits from the in-memory DC into the on-
      // screen DC to actually do the painting. Use the centerpoint
      // we computed for the target offset.
      pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,
         0, 0, SRCCOPY);

      dcMemory.SelectObject(pOldBitmap);
   }

How many other ways are there to load an image?

The image handle is local only and I was trying to avoid any globals.

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 38325755
Well, there are a lot ways... but anyway, that's pretty much what you need to know. The coordinates in

      int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
      int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;

Open in new window


are what you need. Use them with 'ClientToScreen()' and you'll have the absolute location of the image on your desktop.
0
 

Author Comment

by:atomicgs12
ID: 38325795
If feel like this is some kind of joke.
"Well, there are a lot of ways.." OK name one or two.
 
bmpInfo is a local variable only. From another function, unrelated to the function which holds the code I posted, I want to get the bmp handle to the currently loaded picture/image. So the original problem still holds, how do I get the handle to the currently loaded image and from there then we can calculate the position.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 450 total points
ID: 38325862
You've answered your own question.  Since the variables are local, you can't get them outside the function.  If you need that information then store them with a higher scope, or somewhere else, so they are accessible.

When the image is blitted to the screen with BitBlt(), it becomes simply pixels.  You can't get the bitmap information back from the screen itself, much less it's location.

The bitmap could have also been displayed in any number of controls, without using BitBlt()...
0
 

Author Comment

by:atomicgs12
ID: 38325904
OK then that answers my question(s), thanks
0
 
LVL 86

Accepted Solution

by:
jkr earned 1050 total points
ID: 38325944
>>From another function, unrelated to the function which holds the code I posted, I want to
>>get the bmp handle to the currently loaded picture/image.

I assume the image is loaded inside some class or something similar, so why not storing that data in some other class member and use it when it is needed? Or have the the loading function return that as a POINT or something similar?
0

Featured Post

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!

Question has a verified solution.

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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

850 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