Thread: Image(bitmap) larger size than the initial

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    71

    Image(bitmap) larger size than the initial

    I have load a bitmap from a file(image has 2.25 MB) and later save bitmap(it has 3 MB)...why?
    I use this code.
    Code:
    #include <windows.h>
    #include <stdlib.h>
    
    
    PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp);
    void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, 
                      HBITMAP hBMP, HDC hDC);
    
    const char g_szClassName[] = "myWindowClass";
    
    #define DESEN_LINIE 2001
    #define DESEN_TRIUNGHI 2002
    #define DESEN_DREPTUNGHI 2003
    #define DESEN_PATRAT 2004
    #define DESEN_PUNCT 2005
    
    #define FILE_OPEN 3001
    #define FILE_SAVE 3002
    #define FILE_EXIT 3003
    
    int i=0,j=0;
    int cScreenX,cScreenY;
    POINTS ptsLinie;
    
    #include <windows.h>
    #include "abc.h"
    
    HINSTANCE hInst;
    LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX  WndCls;
        static char szAppName[] = "BitmapIntro";
        MSG         Msg;
    
    	hInst       = hInstance;
        WndCls.cbSize        = sizeof(WndCls);
        WndCls.style         = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
        WndCls.lpfnWndProc   = WindProcedure;
        WndCls.cbClsExtra    = 0;
        WndCls.cbWndExtra    = 0;
        WndCls.hInstance     = hInst;
        WndCls.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        WndCls.hCursor       = LoadCursor(NULL, IDC_ARROW);
        WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        WndCls.lpszMenuName  = NULL;
        WndCls.lpszClassName = szAppName;
        WndCls.hIconSm       = LoadIcon(hInstance, IDI_APPLICATION);
        RegisterClassEx(&WndCls);
    
        CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                              szAppName,
                              "Bitmaps Fundamentals",
                              WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              NULL,
                              NULL,
                              hInstance,
                              NULL);
    
        while( GetMessage(&Msg, NULL, 0, 0) )
        {
            TranslateMessage(&Msg);
            DispatchMessage( &Msg);
        }
    
        return Msg.wParam;
    }
    
    LRESULT CALLBACK WindProcedure(HWND hwnd, UINT Msg,
    			   WPARAM wParam, LPARAM lParam)
    {
        static HDC hdc,memDC;
        static PAINTSTRUCT ps;
        static HBITMAP hBmpOld;
    	static HBITMAP hBmpImage;
    	static PBITMAPINFO pBmpInfo;
    	static POINTS ptsBegin;
        switch(Msg)
        {
    		case WM_CREATE:
    		{
    			HMENU hMenu, hSubMenu;
    			hMenu = CreateMenu();
    
    			hSubMenu=CreatePopupMenu();
    			AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File");
            	AppendMenu(hSubMenu, MF_STRING,FILE_OPEN, "OPEN");		
    			AppendMenu(hSubMenu, MF_STRING,FILE_SAVE, "SAVE");	
    			AppendMenu(hSubMenu, MF_STRING,FILE_EXIT, "EXIT");
    
    			SetMenu(hwnd, hMenu);
    
    
    			hdc=GetDC(hwnd);
    			hBmpImage=(HBITMAP)LoadImage(NULL,"abc.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    			if (hBmpImage==NULL)
    				MessageBox(hwnd,"da","da",MB_OK);
    	    	// Create a memory device compatible with the above DC variable
    	    	memDC = CreateCompatibleDC(hdc);
                // Select the new bitmap
          		hBmpOld=SelectObject(memDC,hBmpImage);
    			cScreenX=GetSystemMetrics(SM_CXSCREEN);
    			cScreenY=GetSystemMetrics(SM_CYSCREEN);
    		}
    		break;
    		case WM_COMMAND:
    		{
    			switch(LOWORD(wParam))
    			{
    				case FILE_SAVE:
    					pBmpInfo=CreateBitmapInfoStruct(hwnd,hBmpImage);
    					CreateBMPFile(hwnd,"blabla2.bmp",pBmpInfo,hBmpImage,hdc);
    				break;
    			}
    		}
    		break;
    		case WM_PAINT:
    	    {
    			hdc = BeginPaint(hwnd, &ps);
    	    	// Copy the bits from the memory DC into the current dc
    	    	BitBlt(hdc, 0, 0,cScreenX,cScreenY,memDC, 0, 0, SRCCOPY);
    			// Restore the old bitmap
    	    	EndPaint(hwnd, &ps);
    			++i;
    	    }
    		break;
    		case WM_MOUSEMOVE:
    		{
    			ptsBegin=MAKEPOINTS(lParam);
    			LineTo(memDC,ptsBegin.x,ptsBegin.y);
    		}
    		break;
    		case WM_DESTROY:
    		{
    			SelectObject(memDC,hBmpOld);
    	    	DeleteDC(memDC);
    	    	DeleteObject(hBmpImage);
    			PostQuitMessage(WM_QUIT);
    		}
    	    break;
    		default:
    	    	return DefWindowProc(hwnd, Msg, wParam, lParam);
        }
        return 0;
    
    
    }
    
    PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp)
    { 
        BITMAP bmp; 
        PBITMAPINFO pbmi; 
        WORD    cClrBits; 
    
        // Retrieve the bitmap color format, width, and height.  
        if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp)) 
    		MessageBox(hwnd,"da","da",MB_OK);
    
        // Convert the color format to a count of bits.  
        cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); 
        if (cClrBits == 1) 
            cClrBits = 1; 
        else if (cClrBits <= 4) 
            cClrBits = 4; 
        else if (cClrBits <= 8) 
            cClrBits = 8; 
        else if (cClrBits <= 16) 
            cClrBits = 16; 
        else if (cClrBits <= 24) 
            cClrBits = 24; 
        else cClrBits = 32;
    	    // Allocate memory for the BITMAPINFO structure. (This structure  
        // contains a BITMAPINFOHEADER structure and an array of RGBQUAD  
        // data structures.)  
    
         if (cClrBits < 24) 
             pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
                        sizeof(BITMAPINFOHEADER) + 
                        sizeof(RGBQUAD) * (1<< cClrBits)); 
    
         // There is no RGBQUAD array for these formats: 24-bit-per-pixel or 32-bit-per-pixel 
    
         else 
             pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
                        sizeof(BITMAPINFOHEADER)); 
    
        // Initialize the fields in the BITMAPINFO structure.  
    
        pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
        pbmi->bmiHeader.biWidth = bmp.bmWidth; 
        pbmi->bmiHeader.biHeight = bmp.bmHeight; 
        pbmi->bmiHeader.biPlanes = bmp.bmPlanes; 
        pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel; 
        if (cClrBits < 24) 
            pbmi->bmiHeader.biClrUsed = (1<<cClrBits); 
    	
        // If the bitmap is not compressed, set the BI_RGB flag.  
        pbmi->bmiHeader.biCompression = BI_RGB; 
    
        // Compute the number of bytes in the array of color  
        // indices and store the result in biSizeImage.  
        // The width must be DWORD aligned unless the bitmap is RLE 
        // compressed. 
        pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8
                                      * pbmi->bmiHeader.biHeight; 
        // Set biClrImportant to 0, indicating that all of the  
        // device colors are important.  
         pbmi->bmiHeader.biClrImportant = 0; 
         return pbmi; 
    }
    
    void CreateBMPFile(HWND hwnd, LPTSTR pszFile, PBITMAPINFO pbi, 
                      HBITMAP hBMP, HDC hDC) 
     { 
         HANDLE hf;                 // file handle  
        BITMAPFILEHEADER hdr;       // bitmap file-header  
        PBITMAPINFOHEADER pbih;     // bitmap info-header  
        LPBYTE lpBits;              // memory pointer  
        DWORD dwTotal;              // total count of bytes  
        DWORD cb;                   // incremental count of bytes  
        BYTE *hp;                   // byte pointer  
        DWORD dwTmp; 
    
        pbih = (PBITMAPINFOHEADER) pbi; 
        lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
    
        if (!lpBits) 
    	{
    		MessageBox(hwnd,"da","da",MB_OK);
    	}
    
        // Retrieve the color table (RGBQUAD array) and the bits  
        // (array of palette indices) from the DIB.  
        if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, 
            DIB_RGB_COLORS)) 
        {
            MessageBox(hwnd,"da","da",MB_OK); 
        }
    
        // Create the .BMP file.  
        hf = CreateFile(pszFile, 
                       GENERIC_READ | GENERIC_WRITE, 
                       (DWORD) 0, 
                        NULL, 
                       CREATE_ALWAYS, 
                       FILE_ATTRIBUTE_NORMAL, 
                       (HANDLE) NULL); 
        if (hf == INVALID_HANDLE_VALUE) 
    		MessageBox(hwnd,"da","da",MB_OK);
        hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M"  
        // Compute the size of the entire file.  
        hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + 
                     pbih->biSize + pbih->biClrUsed 
                     * sizeof(RGBQUAD) + pbih->biSizeImage); 
        hdr.bfReserved1 = 0; 
        hdr.bfReserved2 = 0; 
    
        // Compute the offset to the array of color indices.  
        hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + 
                        pbih->biSize + pbih->biClrUsed 
                        * sizeof (RGBQUAD); 
    
        // Copy the BITMAPFILEHEADER into the .BMP file.  
        if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), 
            (LPDWORD) &dwTmp,  NULL)) 
        {
    		MessageBox(hwnd,"da","da",MB_OK);
        }
    
        // Copy the BITMAPINFOHEADER and RGBQUAD array into the file.  
        if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) 
                      + pbih->biClrUsed * sizeof (RGBQUAD), 
                      (LPDWORD) &dwTmp, ( NULL)))
            MessageBox(hwnd,"da","da",MB_OK);
    
        // Copy the array of color indices into the .BMP file.  
        dwTotal = cb = pbih->biSizeImage; 
        hp = lpBits; 
        if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL)) 
               MessageBox(hwnd,"da","da",MB_OK); 
    
        // Close the .BMP file.  
         if (!CloseHandle(hf)) 
               MessageBox(hwnd,"da","da",MB_OK); 
    
        // Free memory.  
        GlobalFree((HGLOBAL)lpBits);
    }
    I guess i have wrong on call of BitBlt(hdc, 0, 0,cScreenX,cScreenY,memDC, 0, 0, SRCCOPY);
    I must put the Height and the width of my bitmap?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps it was RLE encoded to make it smaller.
    BMP file format - Wikipedia, the free encyclopedia

    Perhaps it gained some "depth" (say 24 bit to 32bit) in the process of reading a (compressed) file into (expanded) memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    aaaa...I thought something was wrong in my code.I'll read about it(RLE).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmap Image In C
    By snayyer in forum C Programming
    Replies: 9
    Last Post: 06-20-2011, 10:16 AM
  2. Need larger shmget() size.
    By endomlic in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 02:22 PM
  3. how to convert a bitmap image to a jpg image file using C++?
    By nomer in forum Windows Programming
    Replies: 4
    Last Post: 06-04-2006, 07:40 PM
  4. Reading a bitmap image
    By usamaalam in forum C Programming
    Replies: 19
    Last Post: 03-06-2004, 06:13 AM
  5. Display a bitmap image
    By MysticMizra in forum Windows Programming
    Replies: 7
    Last Post: 10-21-2002, 03:36 AM