Discussion:
How to loadImage from resource
(too old to reply)
Kgbol
2006-12-04 22:23:41 UTC
Permalink
I tried to do it like this, but this is wrong.

HBITMAP hBmp = (HBITMAP)::LoadImage(
NULL,
szFilename,
MAKEINTRESOURCE(IDB_BITMAP1), \\<-here doesn't
works
0,
0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION
);*/
//HINSTANCE hinst;
/*switch (BmpChange)
{
case true :
{szFilename (szFilename1);
BmpChange = false;
break;}
case false:
{szFilename (szFilename2);
BmpChange= true;
break;}
Igor Tandetnik
2006-12-04 22:30:20 UTC
Permalink
Post by Kgbol
I tried to do it like this, but this is wrong.
HBITMAP hBmp = (HBITMAP)::LoadImage(
NULL,
szFilename,
MAKEINTRESOURCE(IDB_BITMAP1), \\<-here doesn't
works
0,
0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION
);*/
It's all wrong. It should be something like this;

LoadImage(hInstance, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP,
0, 0, LR_CREATEDIBSECTION);
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
Kgbol
2006-12-09 17:14:55 UTC
Permalink
Ok I finally loaded the image from resource and displayed it on a
screen using this code:

r_Bitmap=IDB_BITMAP1;

HBITMAP hBmp = (HBITMAP)::LoadImage(
GetModuleHandle(NULL),
MAKEINTRESOURCE(r_Bitmap),
IMAGE_BITMAP,
0,
0,
LR_CREATEDIBSECTION|LR_SHARED
);


CBitmap bmp;
bmp.Attach(hBmp);

CClientDC dc(this);
CDC bmDC;
bmDC.CreateCompatibleDC(&dc);
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);

BITMAP bi;
bmp.GetBitmap(&bi);

dc.BitBlt(0,0,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);

bmDC.SelectObject(pOldbmp);

And the question is:
How to modify this code that i could choose betwen 2 bitmaps which i
would like to display. For example if I click on a button a
IDC_Bitmap_1 would be displayed and after a second click IDC_Bitmap_2
is displayed instead of IDC_Bitmap_1.
MrAsm
2006-12-09 22:56:55 UTC
Permalink
Post by Kgbol
Ok I finally loaded the image from resource and displayed it on a
[...CUT...]
How to modify this code that i could choose betwen 2 bitmaps which i
would like to display. For example if I click on a button a
IDC_Bitmap_1 would be displayed and after a second click IDC_Bitmap_2
is displayed instead of IDC_Bitmap_1.
I don't know if I understand your question well...

However, I would do as follow:

1. Pre-load both bitmaps (the one corresponding to IDC_Bitmap_1 and
the one corresponding to IDC_Bitmap_2). Use ::LoadImage() for this.

You could store these bitmaps in member variables, like:

CBitmap m_bmp1;
CBitmap m_bmp2;

2. Store a pointer to "current" bitmap, e.g.

CBitmap * m_pBmpCurrent;

3. When you handle the "button clicked" event, you adjust the pointer
to point to proper bitmap
Post by Kgbol
CClientDC dc(this);
CDC bmDC;
bmDC.CreateCompatibleDC(&dc);
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
... bmp.SelectObject( m_pBmpCurrent );

Mr Asm
Kgbol
2006-12-12 15:08:38 UTC
Permalink
Thanks Mr Asm, you understand me right. But I have a problem to
implement this code. I created another button that changes between
bitmap1 and bitmap2 (pointer "m_BmpCurrent" points to bmp1 or bmp2)
Below is the code of that button:

void CdisplaySwitchBmpDlg::OnBnClickedButton2()
{
if(BmpChange==true)
{m_BmpCurrent=new CBitmap;
m_BmpCurrent=&bmp1;
BmpChange=false;
}

if(BmpChange==false)
{m_BmpCurrent=new CBitmap;
m_BmpCurrent=&bmp2;
BmpChange=true;
}

}
It works just fine, the problem starts when i try to use delete
function on pointer "m_BmpCurrent". I try to delete it after drawing
the bitmap:


void CdisplaySwitchBmpDlg::OnBnClickedButton1()
{
.......loading images and displaing bitmaps function

delete m_BmpCurrent;

}
when i try to delete m_BmpCurrent i get a pop up window:
Debug assertion failed!
_CtrlsValidHeapPointer(pUserData)

Where is the mistake? Becouse I can't find it.

Loading...