|
-
January 14th, 2002, 03:32 AM
#1
How to display a bitmap on a dialog in runtime?
Hi all,
I want to display a bitmap which is created by the program and want it to be displayed on a dialog box when the program is still drawing the bitmap. I'd tried for long time but seems no work. I'd made a source code like this:
BITMAP bm;
m_ImageWidth = 128;
m_ImageHeight = 128;
m_hC340Image = ::CreateBitmap( m_ImageWidth, m_ImageHeight, 1, 24, NULL );
//Set Initial Value of Bitmap to RGB(255,0,0)
::GetObject( m_hC340Image, sizeof( BITMAP ), &bm );
unsigned char* pData = new unsigned char
[ bm.bmHeight*bm.bmWidthBytes ];
for( j=0; j<bm.bmHeight; j++ )
for( i=0; i<bm.bmWidth; i++ )
{
pData[ i*3+j*bm.bmWidthBytes ] = 0; //B
pData[ i*3+1+j*bm.bmWidthBytes ] = 0; //G
pData[ i*3+2+j*bm.bmWidthBytes ] = 255; //R
}
::SetBitmapBits( m_hC340Image, bm.bmHeight*bm.bmWidthBytes, pData );
delete [] pData;
//show image
m_ctrlC340Image.SetBitmap( m_hC340Image );
UpdateData(false);
The above is part of the sourcecode of a dialog box. The result should be a red square on a dialog, but none was drawn. However, when I use LoadBitmap() function, a bitmap can be shown on the dialog correctly. What's wrong with it? Could anyone please kind to tell me how to make the code?
-
January 14th, 2002, 03:45 AM
#2
Re: How to display a bitmap on a dialog in runtime?
Hi,
Try this..
BITMAP bm;m_ImageWidth = 128;
m_ImageHeight = 128;
m_hC340Image = ::CreateBitmap( m_ImageWidth, m_ImageHeight, 1, 24, NULL );
//Set Initial Value of Bitmap to RGB255,0,0)
::GetObject( m_hC340Image, sizeof( BITMAP ), &bm );
unsigned char* pData = new unsigned char [ bm.bmHeight*bm.bmWidthBytes ];
for( j=0; j<bm.bmHeight; j++ )
for( i=0; i<bm.bmWidth; i++ )
{
pData[ i+j*3*bm.bmWidthBytes ] = 0; //B
pData[ i+1+j*3*bm.bmWidthBytes ] = 0; //G
pData[ i+2+j*3*bm.bmWidthBytes ] = 255; //R }
::SetBitmapBits( m_hC340Image, bm.bmHeight*bm.bmWidthBytes, pData );
delete [] pData;
//show image
m_ctrlC340Image.SetBitmap( m_hC340Image );
UpdateData(false);
Regards,
Mansukh
If you get satisfactory answer, don't forget to RATE
this article.
-
January 14th, 2002, 04:18 AM
#3
Re: How to display a bitmap on a dialog in runtime?
Hi,
I am sorry that this code can not work because the pData[] array is not allocated properly. Could anyone give me some other hints?
unsigned char* pData = new unsigned char [ bm.bmHeight*bm.bmWidthBytes ];
for( j=0; j<bm.bmHeight; j++ )
for( i=0; i<bm.bmWidth; i++ )
{
pData[ i+j*3*bm.bmWidthBytes ] = 0; //B
pData[ i+1+j*3*bm.bmWidthBytes ] = 0;//G
pData[ i+2+j*3*bm.bmWidthBytes ] = 255; //R
}
-
January 14th, 2002, 04:21 AM
#4
Re: How to display a bitmap on a dialog in runtime?
You are using 24 bits per pixel(3 bytes)
unsigned char* pData = new unsigned char [ bm.bmHeight*bm.bmWidthBytes *3];
Regards,
Mansukh
If you get satisfactory answer, don't forget to RATE
this article.
-
January 14th, 2002, 04:37 AM
#5
Re: How to display a bitmap on a dialog in runtime?
Hi,
I did use 24 bit per pixel( 3 bytes).However I set the pData[] array to the sieze of bm.bmHeight*bm.bmWidthBytes which is equal to bm.bmHeight*bm.bmWidth*3. Therefore if I code as you metioned, something may go wrong! But I still thank your suggestion.
-[by Mansukh] ----------------------------------
You are using 24 bits per pixel(3 bytes)
unsigned char* pData = new unsigned char [ bm.bmHeight*bm.bmWidthBytes *3];
-
January 15th, 2002, 06:40 PM
#6
Re: How to display a bitmap on a dialog in runtime?
I am looking for the same.
Did you get any solution on this.
Please rate the article if it is of any use to you. This encourages me to reply more and more and more.
-
January 15th, 2002, 08:59 PM
#7
Re: How to display a bitmap on a dialog in runtime?
OK, This is is what's going on.
1. Most likely you are using a Dialog Common Control, which has a devicecap of pixel bits set to 16. Which means, it will read a bitmap 2bytes by 2bytes instead of 3bytes each time.
If you are not sure about it, try the following code and see the return value:
CDC* dc = GetDlgItem(IDC_BITMAP)->GetDC();
int nBits = ::GetDeviceCaps( dc->GetSafeHdc(), BITSPIXEL);
//Replace the first line with whatever the
//control you are using
2. To make it work, you have two options:
a. Change the bitmap to 16 bit, and truncate your RGB value to 16 bit.
b. Keep the 24 bit settings, and override your common control class's and doe the SelectObject call on your own. (also, you can directly override the dialog and paint over it).
by default, only 16 bits and black white bitmap will show on a Image Control. (oh, if you want to paint 16 bit bitmaps, don't forget to go to your dialog design page and change the Control Type to "Bitmap" the default "Frame" type won't paint any bitmaps).
Hope this helps,
Turtle
-------------------------------------------
Some people tend to use the word "High Tech" or "Cutting Edge" as punctuations in their
sentences. Ignore these words, then you
become a better programmer.
-
January 16th, 2002, 09:01 PM
#8
Re: How to display a bitmap on a dialog in runtime?
Now I use another way to display image in runtime. Say, DC( Device Context ).Following these steps:
(1) Get the display DC:
HWND hwndRenderWindow = GetDlgItem(IDC_C340IMAGE)->GetSafeHwnd();
m_hdcDisplay = ::GetWindowDC(hwndRenderWindow);
(2) Initialize Memory DC
HWND hWnd = GetSafeHwnd();
HDC hdcScreenSet = ::GetWindowDC(hWnd);
HBITMAP hBmpFmtSet = ::CreateCompatibleBitmap(hdcScreenSet,m_dwWidth, m_dwHeight );
m_hdcMemory = ::CreateCompatibleDC( hdcScreenSet );
SelectObject (m_hdcMemory, hBmpFmtSet );
(3) Setpixel in runtime
::SetPixel( m_hdcMemory, dwX, dwY, RGB(dwR,dwG,dwB) );
(4) Display to screen
BitBlt(m_hdcDisplay,2,2,m_dwWidth,m_dwHeight,m_hdcMemory,0,0,SRCCOPY);
Hope this will help~
pcman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|