CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2013
    Posts
    20

    Problem with LoadBitmap using MAKEINTRESOURCE

    Im using Visual Studios 2012 using c++ I have been trying to load a bitmap from my resources
    Like this

    Code:
    HRESULT DemoApp::CreateDIBFromResource(HWND hWnd, HINSTANCE Instance, LPCWSTR ImageID)
    {
    	HRESULT hr = S_OK;
    
    	HBITMAP bitmap = (HBITMAP)LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(ImageID));
    
    	
        
    
        return hr;
    }


    and calling the function like this

    Code:
    if(SUCCEEDED(CreateDIBFromResource(hWnd,NULL, MAKEINTRESOURCE(IDB_BACKGROUND))))
    {
      InvalidateRect(hWnd, NULL, TRUE);
    }
    It wasn't loading anything so I put a breakpoint at
    Code:
    return hr;
    when I hover the cursor over ImageID it says
    ImageID = 0x00000065 <Error reading characters of string.>

    I cant figure out the problem I have Unicode enabled I don't know if that has anything to do with it.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem with LoadBitmap using MAKEINTRESOURCE

    Quote Originally Posted by william3711 View Post
    Im using Visual Studios 2012 using c++ I have been trying to load a bitmap from my resources
    Like this

    Code:
    HRESULT DemoApp::CreateDIBFromResource(HWND hWnd, HINSTANCE Instance, LPCWSTR ImageID)
    {
    	HRESULT hr = S_OK;
    	HBITMAP bitmap = (HBITMAP)LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(ImageID));
    
        return hr;
    }

    and calling the function like this

    Code:
    if(SUCCEEDED(CreateDIBFromResource(hWnd,NULL, MAKEINTRESOURCE(IDB_BACKGROUND))))
    {
      InvalidateRect(hWnd, NULL, TRUE);
    }
    It is absolutely unclear from this code snippet how you are going to use the loaded bitmap. Note that your HBITMAP bitmap is a local variable within CreateDIBFromResource method!
    Besides, your HRESULT hr was set to S_OK, so CreateDIBFromResource will always succeede! But why did you implement such a "design"?

    Quote Originally Posted by william3711 View Post
    It wasn't loading anything
    How did you find out that it "wasn't loading anything"? Did you debug your code and check the return value of LoadBitmap API?


    Quote Originally Posted by william3711 View Post
    ... I put a breakpoint at
    Code:
    return hr;
    when I hover the cursor over ImageID it says
    ImageID = 0x00000065 <Error reading characters of string.>
    And what does it have to do with the loading the bitmap?
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2013
    Posts
    20

    Re: Problem with LoadBitmap using MAKEINTRESOURCE

    Quote Originally Posted by VictorN View Post
    It is absolutely unclear from this code snippet how you are going to use the loaded bitmap. Note that your HBITMAP bitmap is a local variable within CreateDIBFromResource method!
    Besides, your HRESULT hr was set to S_OK, so CreateDIBFromResource will always succeede! But why did you implement such a "design"?

    How did you find out that it "wasn't loading anything"? Did you debug your code and check the return value of LoadBitmap API?


    And what does it have to do with the loading the bitmap?
    Sorry there was more to that function then what I put in the post I just thought that was where the issue was. I used this function to create a DIBSection from a file

    Code:
    HRESULT DemoApp::CreateDIBFromFile(HWND hWnd)
    {
        HRESULT hr = S_OK;
        
        WCHAR szFileName[MAX_PATH];
    
        // Step 1: Create the open dialog box and locate the image file
        if (LocateImageFile(hWnd, szFileName, ARRAYSIZE(szFileName)))
        {
            IWICBitmapDecoder *pDecoder = NULL;
           
            // Step 2: Decode the source image to IWICBitmapSource
    
            // Create a decoder
            hr = m_pIWICFactory->CreateDecoderFromFilename(
                szFileName,                      // Image to be decoded
                NULL,                            // Do not prefer a particular vendor
                GENERIC_READ,                    // Desired read access to the file
                WICDecodeMetadataCacheOnDemand,  // Cache metadata when needed
                &pDecoder                        // Pointer to the decoder
                );
    
            IWICBitmapFrameDecode *pFrame = NULL;
    
            // Retrieve the first frame of the image from the decoder
            if (SUCCEEDED(hr))
            {
                hr = pDecoder->GetFrame(0, &pFrame);
            }
    
            // Retrieve IWICBitmapSource from the frame
            if (SUCCEEDED(hr))
            {
                SafeRelease(m_pOriginalBitmapSource);
                hr = pFrame->QueryInterface(
                    IID_IWICBitmapSource, 
                    reinterpret_cast<void **>(&m_pOriginalBitmapSource));
            }
    
            IWICBitmapSource *pToRenderBitmapSource = NULL;
    
            // Step 3: Scale the original IWICBitmapSource to the client rect size
            // and convert the pixel format
            if (SUCCEEDED(hr))
            {
                hr = ConvertBitmapSource(hWnd, &pToRenderBitmapSource);
            }
    
            // Step 4: Create a DIB from the converted IWICBitmapSource
            if (SUCCEEDED(hr))
            {
                hr = CreateDIBSectionFromBitmapSource(pToRenderBitmapSource);
            }
    
            SafeRelease(pToRenderBitmapSource);
            SafeRelease(pDecoder);
            SafeRelease(pFrame);
        }
    
        return hr;
    }
    Here are the other functions called from this

    Code:
    HRESULT DemoApp::ConvertBitmapSource(HWND hWnd, IWICBitmapSource **ppToRenderBitmapSource)
    {
        *ppToRenderBitmapSource = NULL;
    
        HRESULT hr = S_OK;
    
        // Get the client Rect
        RECT rcClient;
    
        hr = GetClientRect(hWnd, &rcClient) ? S_OK: E_FAIL;
    
        if (SUCCEEDED(hr))
        {
            // Create a BitmapScaler
            IWICBitmapScaler *pScaler = NULL;
    
            hr = m_pIWICFactory->CreateBitmapScaler(&pScaler);
    
            // Initialize the bitmap scaler from the original bitmap map bits
            if (SUCCEEDED(hr))
            {
                hr = pScaler->Initialize(
                    m_pOriginalBitmapSource, 
                    rcClient.right - rcClient.left, 
                    rcClient.bottom - rcClient.top, 
                    WICBitmapInterpolationModeFant);
            }
    
            // Format convert the bitmap into 32bppBGR, a convenient 
            // pixel format for GDI rendering 
            if (SUCCEEDED(hr))
            {
                IWICFormatConverter *pConverter = NULL;
    
                hr = m_pIWICFactory->CreateFormatConverter(&pConverter);
    
                // Format convert to 32bppBGR
                if (SUCCEEDED(hr))
                {
                    hr = pConverter->Initialize(
                        pScaler,                         // Input bitmap to convert
                        GUID_WICPixelFormat32bppBGR,     // Destination pixel format
                        WICBitmapDitherTypeNone,         // Specified dither patterm
                        NULL,                            // Specify a particular palette 
                        0.f,                             // Alpha threshold
                        WICBitmapPaletteTypeCustom       // Palette translation type
                        );
    
                    // Store the converted bitmap as ppToRenderBitmapSource 
                    if (SUCCEEDED(hr))
                    {
                        hr = pConverter->QueryInterface(IID_PPV_ARGS(ppToRenderBitmapSource));
                    }
                }
    
                SafeRelease(pConverter);
            }
    
            SafeRelease(pScaler);
        }
    
        return hr;
    }

    Code:
    HRESULT DemoApp::CreateDIBSectionFromBitmapSource(IWICBitmapSource *pToRenderBitmapSource)
    {
        HRESULT hr = S_OK;
    
        // Get image attributes and check for valid image
        UINT width = 0;
        UINT height = 0;
    
        void *pvImageBits = NULL;
        
        // Check BitmapSource format
        WICPixelFormatGUID pixelFormat;
        hr = pToRenderBitmapSource->GetPixelFormat(&pixelFormat);
    
        if (SUCCEEDED(hr))
        {
            hr = (pixelFormat == GUID_WICPixelFormat32bppBGR) ? S_OK : E_FAIL;
        }
    
        if (SUCCEEDED(hr))
        {
            hr = pToRenderBitmapSource->GetSize(&width, &height);
        }
    
        // Create a DIB section based on Bitmap Info
        // BITMAPINFO Struct must first be setup before a DIB can be created.
        // Note that the height is negative for top-down bitmaps
        if (SUCCEEDED(hr))
        {
            BITMAPINFO bminfo;
            ZeroMemory(&bminfo, sizeof(bminfo));
            bminfo.bmiHeader.biSize         = sizeof(BITMAPINFOHEADER);
            bminfo.bmiHeader.biWidth        = width;
            bminfo.bmiHeader.biHeight       = -(LONG)height;
            bminfo.bmiHeader.biPlanes       = 1;
            bminfo.bmiHeader.biBitCount     = 32;
            bminfo.bmiHeader.biCompression  = BI_RGB;
    
            // Get a DC for the full screen
            HDC hdcScreen = GetDC(NULL);
    
            hr = hdcScreen ? S_OK : E_FAIL;
    
            // Release the previously allocated bitmap 
            if (SUCCEEDED(hr))
            {
                if (m_hDIBBitmap)
                {
                    DeleteObject(m_hDIBBitmap);
                }
    
                m_hDIBBitmap = CreateDIBSection(hdcScreen, &bminfo, DIB_RGB_COLORS,
                    &pvImageBits, NULL, 0);
    
                ReleaseDC(NULL, hdcScreen);
    
                hr = m_hDIBBitmap ? S_OK : E_FAIL;
            }
        }
    
        UINT cbStride = 0;
        if (SUCCEEDED(hr))
        {
            // Size of a scan line represented in bytes: 4 bytes each pixel
            hr = UIntMult(width, sizeof(ARGB), &cbStride);
        }
        
        UINT cbImage = 0;
        if (SUCCEEDED(hr))
        {
            // Size of the image, represented in bytes
            hr = UIntMult(cbStride, height, &cbImage);
        }
    
        // Extract the image into the HBITMAP    
        if (SUCCEEDED(hr))
        {
            hr = pToRenderBitmapSource->CopyPixels(
                NULL,
                cbStride,
                cbImage, 
                reinterpret_cast<BYTE *> (pvImageBits));
        }
    
        // Image Extraction failed, clear allocated memory
        if (FAILED(hr))
        {
            DeleteObject(m_hDIBBitmap);
            m_hDIBBitmap = NULL;
        }
    
        return hr;
    }
    and heres where CreateDIBFromFile is called

    Code:
    LRESULT DemoApp::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
    
    		
            case WM_COMMAND:
            {
                // Parse the menu selections:
                switch (LOWORD(wParam))
                {
    				
    
                    case IDM_FILE:
                    {
                        if (SUCCEEDED(CreateDIBFromFile(hWnd)))
                        {
                            InvalidateRect(hWnd, NULL, TRUE);
                        }
                        else
                        {
                            MessageBox(NULL, L"Failed to load image, select another one.", L"Application Error", MB_ICONEXCLAMATION | MB_OK);
                        }
                        break;
                    }
    				case IDM_BACK:
    					{
    						if(SUCCEEDED(CreateDIBFromResource(hWnd,NULL, L"back")))
    						{
    							InvalidateRect(hWnd, NULL, TRUE);
    						}
    						break;
    					}
                    case IDM_EXIT:
                    {
                        PostMessage(hWnd, WM_CLOSE, 0, 0); 
                        break;
                    }
                }
                break;
             }
            case WM_SIZE:
            {
                IWICBitmapSource *pToRenderBitmapSource;
    
                if (SUCCEEDED(ConvertBitmapSource(hWnd, &pToRenderBitmapSource)))
                {
                    CreateDIBSectionFromBitmapSource(pToRenderBitmapSource);
                    SafeRelease(pToRenderBitmapSource);
                }
                break;
            }
            case WM_PAINT:
            {
                return OnPaint(hWnd);
            }
            case WM_DESTROY:
            {
                PostQuitMessage(0);
                break;
            }
            default:
                return DefWindowProc(hWnd, uMsg, wParam, lParam);
        }
        return 0;
    }
    but when I try to create a DIB from a resource using same code but loading a resource with LoadBitMap and yes I Debugged my and checked the return value an I got showed the error I mentioned.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Problem with LoadBitmap using MAKEINTRESOURCE

    Quote Originally Posted by william3711 View Post
    ...
    but when I try to create a DIB from a resource using same code but loading a resource with LoadBitMap and yes I Debugged my and checked the return value an I got showed the error I mentioned.
    What "same code" do you mean? You still do not show it!
    And "the return value" of what did you check? And what was this value?
    Victor Nijegorodov

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Problem with LoadBitmap using MAKEINTRESOURCE

    Quote Originally Posted by william3711 View Post
    when I hover the cursor over ImageID it says
    ImageID = 0x00000065 <Error reading characters of string.>

    I cant figure out the problem I have Unicode enabled I don't know if that has anything to do with it.
    You really couldn't figure out the problem, and ImageID is definitely not the one. MAKEINTRESOURCE macro just makes compiler be happy about passing UINT pretending to be a LPCWSTR, and nothing else.
    Looking at ImageID = 0x00000065 I can conclude your IDB_BACKGROUND is defined as 101.
    Best regards,
    Igor

Tags for this Thread

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