Loading bitmap in particular way
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Loading bitmap in particular way

  1. #1
    Join Date
    May 2013
    Posts
    1

    Loading bitmap in particular way

    I have a function that essentially takes a screen shot and saves a pointer to it as a structure. I would like to use the same structure for bitmaps that I load from files.

    Code:
    typedef struct _BITMAPCAPTURE {
    HBITMAP hbm;
    LPDWORD pixels;
    INT     width;
    INT     height;
    } BITMAPCAPTURE;
    
    BOOL CaptureScreen(BITMAPCAPTURE* bmpCapture)
    {
    BOOL bResult = FALSE;
    if(!bmpCapture)
        return bResult;
    
    ZeroMemory(bmpCapture, sizeof(BITMAPCAPTURE));
    
    HDC hdcScreen  = GetDC(NULL);
    HDC hdcCapture = CreateCompatibleDC(NULL);
    int nWidth     = GetSystemMetrics(SM_CXVIRTUALSCREEN),
        nHeight    = GetSystemMetrics(SM_CYVIRTUALSCREEN);
    
    // Bitmap is stored top down as BGRA,BGRA,BGRA when used as
    // DWORDs endianess would change it to ARGB.. windows COLORREF is ABGR
    LPBYTE lpCapture;
    BITMAPINFO bmiCapture = { {
        sizeof(BITMAPINFOHEADER), nWidth, -nHeight, 1, 32, BI_RGB, 0, 0, 0, 0, 0,
    } };
    
    bmpCapture->hbm = CreateDIBSection(hdcScreen, &bmiCapture,
        DIB_RGB_COLORS, (LPVOID *)&lpCapture, NULL, 0);
    if(bmpCapture->hbm){
        HBITMAP hbmOld = (HBITMAP)SelectObject(hdcCapture, bmpCapture->hbm);
        BitBlt(hdcCapture, 0, 0, nWidth, nHeight, hdcScreen, 0, 0, SRCCOPY);
        SelectObject(hdcCapture, hbmOld);
        bmpCapture->pixels = (LPDWORD)lpCapture;
        bmpCapture->width  = nWidth;
        bmpCapture->height = nHeight;
        bResult = TRUE;
    }
    
    DeleteDC(hdcCapture);
    DeleteDC(hdcScreen);
    return bResult;
    }
    The handle to the bitmap, as well as the width and height are all easy enough to get but I'm unsure of how to get the pixels. Thanks very much.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    1,999

    Re: Loading bitmap in particular way

    Quote Originally Posted by IronRhino View Post
    I have a function that essentially takes a screen shot and saves a pointer to it as a structure. I would like to use the same structure for bitmaps that I load from files.
    What's wrong with CImage::Load?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,881

    Re: Loading bitmap in particular way

    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    
    typedef struct _BITMAPCAPTURE {
    HBITMAP hbm;
    LPDWORD pixels;
    INT     width;
    INT     height;
    } BITMAPCAPTURE, *PBITMAPCAPTURE;
    
    BOOL LoadBitmapAndGetCaps(LPCTSTR fileName, PBITMAPCAPTURE cap)
    {
    	HBITMAP hbm = (HBITMAP)LoadImage(NULL, fileName, IMAGE_BITMAP, 
    		LR_DEFAULTSIZE, LR_DEFAULTSIZE, 
    		LR_CREATEDIBSECTION | LR_LOADFROMFILE | LR_DEFAULTSIZE);
    
    	BITMAP bm = {0};
    
    	if (hbm && GetObject((HGDIOBJ)hbm, sizeof(bm), &bm))
    	{
    		cap->hbm    = hbm;
    		cap->pixels = (LPDWORD)bm.bmBits;
    		cap->width  = bm.bmWidth;
    		cap->height = bm.bmHeight;
    
    		return TRUE;
    	}
    
    	return FALSE;
    }
    
    int _tmain(int argc, TCHAR** argv)
    {
    	if (argc)
    	{
    		BITMAPCAPTURE bmc = {0};
    		BOOL success = LoadBitmapAndGetCaps(argv[1], &bmc);
    		if (success)
    			_tprintf(TEXT("Bitmap width = %d, height = %d, bits = 0x%p"), 
    				bmc.width, bmc.height, bmc.pixels);
    		else
    			_tprintf(TEXT("Failure with last error %d"), GetLastError());
    
    	}
    
    	return 0;
    }
    Code:
    E:\Temp\692>692.exe "C:\Users\Igor\Local Settings\Temp\Microsoft Visual Studio 2010 Service Pack 1_10.0.40219\SplashScreen.bmp"
    Bitmap width = 64, height = 64, bits = 0x002D0000
    Best regards,
    Igor

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center