CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2009

    why i can't create a memory HBitmap and draw it?

    heres how i get the console HDC:
    //getting the HDC Console Window:
        HDC WindowHDC=GetDC(GetConsoleWindow());
    works fine, because the SetPixel() draw the right pixel.
    heres my actual image class:
    class image{
        BITMAP bitmap;
        HBITMAP hBitmap;
        HDC hdcMem;
        HDC oldBitmap;
            hdcMem = CreateCompatibleDC(GetDC(NULL));
            oldBitmap =(HDC) SelectObject(hdcMem, hBitmap);
            SelectObject(hdcMem, oldBitmap);
        void FromFile(string strFile)
            hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL),
             oldBitmap =(HDC) SelectObject(hdcMem, hBitmap);
             GetObject(hBitmap, sizeof(bitmap), &bitmap);
        void DrawImage(HDC DestinationHDC)
             BitBlt(DestinationHDC, 0, 0, bitmap.bmWidth, bitmap.bmHeight,hdcMem, 0, 0, SRCCOPY);
             //MessageBox(NULL,"error: BITBLT", to_string(GetLastError()).c_str(), MB_OK);
    see these function:
    void FromFile(string strFile)    {
            hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL),
            if(hBitmap==NULL) MessageBox(NULL,"error: BITBLT", to_string(GetLastError()).c_str(), MB_OK);
    i get the message box with '0'(zero).... my question: why the HBitmap is NULL?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: why i can't create a memory HBitmap and draw it?

    [Also asked and answered here ]
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.4.4)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width