Problems with Bitmap and HBITMAP

Posted on 2009-04-29
Last Modified: 2013-12-03

I'm getting a runtime error on the indicated line in the code below, the problem is that p is NULL as is data.Scan0. Below is the debug output of the variable data members:

Width            3435973836      unsigned int
Height            3435973836      unsigned int
Stride            -858993460      int
PixelFormat      -858993460      int
Scan0            0xcccccccc      void *
Reserved      3435973836      unsigned int

I'm guessing that somewhere down the line that data isn't being assigned to the BitmapData correctly. Anybody care to help?

void CScreenCapture::initialise(unsigned int aWidth, unsigned int aHeight, unsigned int aPosX, unsigned int aPosY){






	hCapturedScreen=CreateCompatibleBitmap(GetDC(NULL), width, height);


void CScreenCapture::captureScreen(){

	SelectObject(hDC, hCapturedScreen);

	BitBlt(hDC, 0, 0, width, height, GetDC(NULL), posX, posY, SRCCOPY);



void CScreenCapture::convertPixels(){

	Bitmap bitmap(hCapturedScreen, NULL);

	BitmapData data;

	Rect rect(0, 0, bitmap.GetWidth(), bitmap.GetHeight());

	if(bitmap.LockBits(&rect, ImageLockModeRead, PixelFormat24bppRGB, &data)){

		LPBYTE pPixelData=(LPBYTE)data.Scan0;

		unsigned char *p;


		for(unsigned int y=0;y<data.Height;y++){

			p=((unsigned char*)data.Scan0)+(y*data.Stride);

			vector<COLORREF> temp;

			for(unsigned int x=0;x<data.Width;x++,p+=3)

				temp.push_back(RGB(p[2], p[1], p[0])); /////////////////// ERROR HERE.






Open in new window

Question by:Unimatrix_001
    LVL 9

    Accepted Solution

    LockBits returns Ok (which is 0) on success, not true, so your LockBits is failing.  This would happen if you are not calling GdiplusStartup to initialize gdi+:
    LVL 3

    Author Comment

    Ahh... Yes, that seems to be it. :)
    LVL 3

    Author Closing Comment

    10/10. Thank you. :)
    LVL 9

    Expert Comment

    There is another possible problem.  In the documentation for the Bitmap constructor that takes a HBITMAP, it says:

    Do not pass to the GDI+ Bitmap constructor a GDI bitmap or a GDI palette that is currently (or was previously) selected into a device context.

    And it seems that you are passing in a HBITMAP that is still selected into DC.  You need to save the return from your SelectObject(hDC, hCapturedScreen) and then select that back into the DC after you do your BitBlt.

    Also, as a final note, you are using CreateCompatibleBitmap to create your bitmap.  I am not sure that that will yield a Device Independent bitmap.  If you have trouble with it, you might consider using CreateDIBSection instead.
    LVL 3

    Author Comment

    Hm, to be honest I'm not too clever with this sort of windows programming, I've just cobbled things together from things I've picked up over the net, although it seems to be working fine for now. Nevertheless, I'll see about making the changes you've proposed.

    Thank you,
    LVL 9

    Expert Comment

    If your CreateCompatibleBitmap is working, I see not need to change that.  I only mentioned it in case you had problems.

    However, I would fix the issue with the bitmap being selected into the DC when you create the Bitmap, since the documentation specifically tells you not to do that.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
    As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now