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

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

    heres how i get the console HDC:
    Code:
    //getting the HDC Console Window:
        HDC WindowHDC=GetDC(GetConsoleWindow());
    works fine, because the SetPixel() draw the right pixel.
    heres my actual image class:
    Code:
    class image{
        public:
        BITMAP bitmap;
        HBITMAP hBitmap;
        HDC hdcMem;
        HDC oldBitmap;
    
    
        image()
        {
            hdcMem = CreateCompatibleDC(GetDC(NULL));
            oldBitmap =(HDC) SelectObject(hdcMem, hBitmap);
        }
    
    
        ~image()
        {
            SelectObject(hdcMem, oldBitmap);
            DeleteDC(hdcMem);
        }
    
    
        void FromFile(string strFile)
        {
            hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL),
            strFile.c_str(),IMAGE_BITMAP,LR_DEFAULTSIZE,LR_DEFAULTSIZE,LR_LOADFROMFILE);
             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:
    Code:
    void FromFile(string strFile)    {
            hBitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL),
            strFile.c_str(),IMAGE_BITMAP,LR_DEFAULTSIZE,LR_DEFAULTSIZE,LR_LOADFROMFILE);
            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
    Location
    England
    Posts
    7,610

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

    [Also asked and answered here http://www.cplusplus.com/forum/general/281591/ ]
    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 https://creativecommons.org/publicdomain/zero/1.0/ 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.2.2)

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

Featured