I'm stupid help me 
Author Message
 I'm stupid help me

Normally I would create a CBitmap and load it with:

CBitmap::Attach(::LoadImage(NULL, lpszPathName, IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE));

but I want to be able to create a new bitmap and set each pixel individually
so I tried using:

 char data[256*256*3];
 memset(data, 256, 256*256*3);
 m_WBitmap.CreateBitmap( 256, 256, 1, 24, data );

but this doesn't work I draw the bitmap with the following function:

void CWatermarkBitmap::Draw(CDC* pDC)
{
 CDC dcMemory;
 dcMemory.CreateCompatibleDC(pDC);

 CBitmap *pbmOriginal = dcMemory.SelectObject(this);

 CRect rcClient;
 GetClientRect(rcClient);

 pDC->StretchBlt(0, 0, rcClient.Width(), rcClient.Height(), &dcMemory, 0, 0,
48, 48,SRCCOPY);

 pDC->SelectObject(pbmOriginal);

Quote:
}

Normally this would work but it doesnt matter what I set the contents of my
new bitmap to it never draws anything. I am quite new to MFC and I cant find
any examples of CreateBitmap that take data the only ones that I cant find
always use NULL to create an empty bitmap.

Thanks for the help

Col



Wed, 26 Mar 2003 03:00:00 GMT  
 I'm stupid help me
Don't worry, it's one of those things where you either know it, or you
don't. It can't really be worked out ;-)

Anyway, the solution is as follows:
The trouble is that your bitmap is not compatible with your DC. To fix this
you need to create a compatible bitmap, and assign the bits afterwards. This
can only be done once you have a device context that you are going to draw
to. The following code should enable you to build it in advance...

1.    Get a handle to the device context that you will draw to.
    CDC * pDC = AfxGetMainWnd()->GetDC();

2.    Create a compatible bitmap.
    m_WBitmap.DeleteObject();        // In case this is not the first
creation
    m_WBitmap.CreateCompatibleBitmap(pDC, 256, 256);    // 256x256 bitmap

3.    Allocate your data. For simplicity's sake, we'll assume that you are
using 24bit colour)
    BYTE pData[256*256*3];
    memset(pData, 255, 256*256*3);    // note 255, not 256 as BYTES go from
0 - 255

4.    Now assign the data to the bitmap
    m_WBitmap.SetBitmapBits(256*256*3, pData);

5. Now you should be able to stretchblt in the normal way :-)

Jase


Quote:
> Normally I would create a CBitmap and load it with:

> CBitmap::Attach(::LoadImage(NULL, lpszPathName, IMAGE_BITMAP, 0, 0,
> LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE));

> but I want to be able to create a new bitmap and set each pixel
individually
> so I tried using:

>  char data[256*256*3];
>  memset(data, 256, 256*256*3);
>  m_WBitmap.CreateBitmap( 256, 256, 1, 24, data );

> but this doesn't work I draw the bitmap with the following function:

> void CWatermarkBitmap::Draw(CDC* pDC)
> {
>  CDC dcMemory;
>  dcMemory.CreateCompatibleDC(pDC);

>  CBitmap *pbmOriginal = dcMemory.SelectObject(this);

>  CRect rcClient;
>  GetClientRect(rcClient);

>  pDC->StretchBlt(0, 0, rcClient.Width(), rcClient.Height(), &dcMemory, 0,
0,
> 48, 48,SRCCOPY);

>  pDC->SelectObject(pbmOriginal);
> }

> Normally this would work but it doesnt matter what I set the contents of
my
> new bitmap to it never draws anything. I am quite new to MFC and I cant
find
> any examples of CreateBitmap that take data the only ones that I cant find
> always use NULL to create an empty bitmap.

> Thanks for the help

> Col



Sat, 29 Mar 2003 13:29:05 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. help: basic stupid questions, please help tho

2. stupid bug that i didn't see

3. Emulov's Stupid Questions: nr25 (I think, I lost count)

4. Emulov's stupid questions: nr 24

5. Emulov's stupid questions: nr 23

6. Emulov's Stupid Questions: nr 22

7. Ok, I know it's stupid,but..[Preprocessing]

8. Stupid Beginners' Question

9. Proof that I am stupid -- newbies, don't do this

10. Don't I feel stupid...

11. Awful VC 7.1 Final Beta or I'm stupid

12. Really stupid question about static linking to MFC dll's

 

 
Powered by phpBB® Forum Software