Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote:

CImage image;
image.Load(str_path); // just change extension to load jpg
CBitmap bitmap;
bitmap.Attach(image.Detach());
m_Btn1.SetBitmap(bitmap);
m_Btn1.ShowWindow(SW_SHOW);


str_path is the path of the file jpg

What I have tried:

The image isn't loaded..I searched on internet and I found the code that I wrote
Posted
Updated 19-Apr-24 6:10am
v2

I would guess it is because the image is not accessible to your program. Either give the image file name an explicit path or run your program in the directory where the image file is located.

Actually, the first thing you should do is check the return of the Load method to verify whether the image was actually loaded or not. It is possible that it was loaded but other logic is prohibiting the button from being displayed.
 
Share this answer
 
Comments
Member 14594285 19-Apr-24 12:10pm    
str_path is the path of the file jpg
Rick York 19-Apr-24 13:31pm    
Yes, I know. I read the documentation on the class. https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/reference/cimage-class?view=msvc-170
Member 14594285 22-Apr-24 9:39am    
I loaded image:

CBitmap bitmap;

bitmap.Attach(image.Detach());

HBITMAP hBitmap6 = (HBITMAP)bitmap.GetSafeHandle();

m_Btn1.SetBitmap(hBitmap6);

but image isn't proportionate to size of button
Your code seems to be correct for loading an image and setting it as the bitmap for your button button. As Rick stated, first check if the path you use is correct and if it is returning something,

Add error handling and debugging statements to your code to check if your 'Load' and 'Attach' functions are returning any error codes -

C++
HRESULT hr = image.Load(str_path);
if (FAILED(hr))
{
    //Display a message or log the error code...
    CString errorMessage;
    image.GetLastErrorDescription(errorMessage);
    AfxMessageBox(errorMessage);
}

if (!bitmap.Attach(image.Detach()))
{
    AfxMessageBox(_T("Failed to attach the bitmap."));
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900