• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 803
  • Last Modified:

How to get loaded image coordinates. Windows c++

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
atomicgs12
Asked:
atomicgs12
  • 6
  • 5
  • 2
2 Solutions
 
jkrCommented:
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
 
atomicgs12Author Commented:
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
 
atomicgs12Author Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
jkrCommented:
Thus my question: How are you displaying the image?
0
 
atomicgs12Author Commented:
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
 
jkrCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
LOL...this is an entertaining thread to follow.  The image gets displayed by ~magic~ jkr!  ;)
0
 
atomicgs12Author Commented:
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
 
jkrCommented:
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
 
atomicgs12Author Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
atomicgs12Author Commented:
OK then that answers my question(s), thanks
0
 
jkrCommented:
>>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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now