Link to home
Start Free TrialLog in
Avatar of rosetalk
rosetalk

asked on

How to use LoadBitmap API?


I have downloaded a file from "www.codeguru.com/dialog "under the section list of Image Preview by luis Otega. This file only opens a dialog with a bitmap preview without loading a bitmap. My question is how to implement the LoadBitmap API into the file. What are the parameters to pass to the LoadBitmap function based on his code? Is the LoadBitmap API enough to load the bitmap file that is selected? If not, what else do I need to include?

I would also like to get the filepath of the file selected. I know I need to use the GetPathName function. But how to use it in his file. I have post my questions to the author and yet to hear from him.

Please help me, if you have time to look at his code. It is quite a short one.

From: A Beginner
Avatar of pagladasu
pagladasu

Are you using Visual C++ with MFC or just SDK.
With loadbitmap, you load a bitmap resource. You are returned a bitmap handle. The next step is to create a compatible device context with the client device context with the CreateCompatibleDC function. Next is to select the bitmap handle into this device context. After this use the BitBlt() or related functions to draw the bitmap on the display.
Hope this helps.
Thanks
pagladasu
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Like LoadBitmap(), LoadImage() can load from the resources, but it can also load from a file on disk.  It also can load cursors and icons as well as bitmaps.  So there are lots of options to use it.  But to load a bitmap from a file on disk, specify:

NULL for the hinst parameter.
Specify the file name for the 2nd parameter
Specify IMAGE_BITMAP for the 3rd parameter
Specify the desired width or 0 for the actuall width of the image in the 4th parameter.
Specify the desired height or 0 for the actual height of the image in the 5th parameter.
Specify LR_LOADFROMFILE in the 6th parameter.

Now this creates a device dependant bitmap that is compatible with the display (screen).  (Which is what LoadBitmap() always does)  If you want to create a device independant bitmap you can use the LR_CREATEDIBSECTION flag as well in the last parameter.

Let me know if you have questions.
Neitod is right. For loading files, you need LoadImage(0
Avatar of rosetalk

ASKER

Neitod,
         I have tried your method to use LoadImage(). However, I there is nothing loaded on the client area.I have noticed that the author uses CBmpDialog::hpreview = (HBITMAP)LoadImage(AfxGetInstanceHandle(),filename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION); with these parameters as well to load the image on the preview screen and it works. I uses his parameters and nothing is loaded on the client area as well. I am using MFC with SDK. What could be wrong? The arthor uses hook function in his implementation. Please try out his dialog box if you have time.
>>I would also like to get the filepath of the file selected. I know
>> I need to use the GetPathName function

GetPathName() retirns the name of the file selected by the user.  If you want to load that file, you specify that name to LoadImage().

>> I have tried your method to use LoadImage(). However, I there is
>>nothing loaded on the client area
An image can't be "loaded on the client area".  It is just loaded into memory.  From there it can be copied to a a device context to display the image on the screen or on a printer or other hardware device.  Take a look at codeguru's examples about displaying bitmaps.
Are you getting anywhere on this?
Yes, I manage to contact the author and I am glad that the problem is solved. Anyway, thank for your help. Nietod... But when the picture is loaded, its seems like the screen doest not refresh when I am scrolling. What should I do if let say execute the OnDraw() function to draw the image while scrolling? If the methed is not right, can you give me suggestions.
Do you mean it does not redisplay DURING scrolling or AFTER scrolling?  (I.e after scrolling ends (after the user releases the mouse) does it then look okay?)  

What is scrolling?  Are you scrolling the window that contains the bitmap, or are you scrolling a window that contains a window that has the bitmap.



I mean scrolling the window that contains the bitmap.It does not redisplay DURING scrolling and AFTER scrolling.
Inaddition, I have to add a refresh button on the toolbar inorder to execute the OnDraw function under View class. This will refresh the distorted image after scrolling.
Since you are using MFC, I'm not exactly sure what you should be doing to handle scrolling.  In the Non-MFC world, during the WM_xSCROLL message you would use ScrollWindowEx() to scroll part of the window that is displayed into its new position.  This will invalidate the part of the window that should contain the part that is scrolled into view (the part that wasn't vissible before).  Then a call to UpdateWindow() will redisplay that part.  In MFC, I suspect most of that is taking care of for you.  I would bet that MFC handles the ScrollWindowEx call and that all you would have to do is to repaint your window in onDraw.  but I'm not sure.  However, when you draw in the OnDraw function, your probably have to take the scroll bar position into account and draw the image at the right location.  Are you doing that?
Yes, I taken care of that but it doesn't seems to work either. Are you free to have a look at my program?  
You can e-mail it to me at nietod@theshop.net.  But as I don't use MFC (I know a little) I may not be able to help.