Need code for bmp file

guys need code for reading .bmp files
and saving .bmp files in plain C or C++.

Can anyone help please... asap..

Thanks in advance..

this code saves a bitmap.. have a look and understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
int  SaveBitmap(TCHAR *szBitmapPath)
{

      HDC				hdc=NULL,hdcDesktop,hdcMem;
      FILE*				fp=NULL;
	  HBITMAP			hBitmap;
	  RECT				rectDesktop;
	  HWND				hDesktop;
	  HPALETTE			hPal = NULL;
	  int				retval = SUCCESS;
	  TCHAR				szPath[MAX_PATH];

	 
      do{ 
			
            hDesktop = GetDesktopWindow();
			//hDesktop = hwnd;
			hdcDesktop = GetDC(hDesktop);
			GetClientRect(hDesktop,&rectDesktop);
			hdcMem = CreateCompatibleDC(hdcDesktop);
			hBitmap = CreateCompatibleBitmap(hdcDesktop,rectDesktop.right - rectDesktop.left,
						rectDesktop.bottom - rectDesktop.top);
			HBITMAP oldBitmap = (HBITMAP)SelectObject(hdcMem,hBitmap);
			retval = BitBlt(hdcMem,0,0,rectDesktop.right - rectDesktop.left,rectDesktop.bottom - rectDesktop.top,
				hdcDesktop,0,0,SRCCOPY);
			hBitmap = (HBITMAP)SelectObject(hdcMem,oldBitmap);

			SetCursor(LoadCursor(NULL,IDC_WAIT));

			HANDLE hDib = BitmapToDIB(hBitmap,hPal);
			//SHGetSpecialFolderPath(NULL,szPath,CSIDL_DESKTOPDIRECTORY,0);
			GetTempPath(MAX_PATH, szPath );
			strcat(szPath, "\\temp.jpg");
			retval = SaveDIB(hDib,szPath);
			if(retval == SUCCESS)
			{
				strcpy(szBitmapPath, szPath);
			}

			DeleteDC(hdcMem);
			DeleteDC(hdcDesktop);
			
			SetCursor(LoadCursor(NULL,IDC_ARROW));
			
		}while(false);

	  return retval;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
WORD SaveDIB(HANDLE hDib, LPSTR lpFileName)
{
    BITMAPFILEHEADER    bmfHdr;     // Header for Bitmap file
    LPBITMAPINFOHEADER  lpBI;       // Pointer to DIB info structure
    HANDLE              fh;         // file handle for opened file
    DWORD               dwDIBSize;
    DWORD               dwWritten;

    if (!hDib)
        return ERR_INVALIDHANDLE;
	
    fh = CreateFile(lpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
            FILE_ATTRIBUTE_NORMAL, NULL);

    if (fh == INVALID_HANDLE_VALUE)
        return ERR_OPEN;

    // Get a pointer to the DIB memory, the first of which contains
    // a BITMAPINFO structure

    lpBI = (LPBITMAPINFOHEADER)GlobalLock(hDib);
    if (!lpBI)
    {
        CloseHandle(fh);
        return ERR_LOCK;
    }

    

    if (lpBI->biSize != sizeof(BITMAPINFOHEADER))
    {
        GlobalUnlock(hDib);
        CloseHandle(fh);
        return ERR_NOT_DIB;
    }

    

    bmfHdr.bfType = DIB_HEADER_MARKER;  // "BM"

    

    // Partial Calculation

    dwDIBSize = *(LPDWORD)lpBI + PaletteSize((LPSTR)lpBI);  

    // Now calculate the size of the image

    

    if ((lpBI->biCompression == BI_RLE8) || (lpBI->biCompression == BI_RLE4))
        dwDIBSize += lpBI->biSizeImage;
    else
    {
        DWORD dwBmBitsSize;  // Size of Bitmap Bits only

        

        dwBmBitsSize = WIDTHBYTES((lpBI->biWidth)*((DWORD)lpBI->biBitCount)) *
                lpBI->biHeight;

        dwDIBSize += dwBmBitsSize;

    
        lpBI->biSizeImage = dwBmBitsSize;
    }


    // Calculate the file size by adding the DIB size to sizeof(BITMAPFILEHEADER)
                   
    bmfHdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
    bmfHdr.bfReserved1 = 0;
    bmfHdr.bfReserved2 = 0;

    
    bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + lpBI->biSize +
            PaletteSize((LPSTR)lpBI);

    

    WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);

    
    WriteFile(fh, (LPSTR)lpBI, dwDIBSize, &dwWritten, NULL);

    GlobalUnlock(hDib);
    CloseHandle(fh);

    if (dwWritten == 0)
        return ERR_OPEN; // oops, something happened in the write
    else
        return SUCCESS; // Success code
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
HANDLE BitmapToDIB(HBITMAP hBitmap, HPALETTE hPal)
{
    BITMAP              bm; 
    BITMAPINFOHEADER    bi; 
    LPBITMAPINFOHEADER  lpbi; 
    DWORD               dwLen;
    HANDLE              hDIB, h; 
    HDC                 hDC;
    WORD                biBits; 

    // check if bitmap handle is valid

    if (!hBitmap)
        return NULL;

   

    if (!GetObject(hBitmap, sizeof(bm), (LPSTR)&bm))
        return NULL;

    

    if (hPal == NULL)
        hPal = (HPALETTE)GetStockObject(DEFAULT_PALETTE);

    

    biBits = bm.bmPlanes * bm.bmBitsPixel;

    

    if (biBits <= 1)
        biBits = 1;
    else if (biBits <= 4)
        biBits = 4;
    else if (biBits <= 8)
        biBits = 8;
    else // if greater than 8-bit, force to 24-bit
        biBits = 24;

    // initialize BITMAPINFOHEADER

    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bm.bmWidth;
    bi.biHeight = bm.bmHeight;
    bi.biPlanes = 1;
    bi.biBitCount = biBits;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;

    // calculate size of memory block required to store BITMAPINFO

    dwLen = bi.biSize + PaletteSize((LPSTR)&bi);

    // get a DC

    hDC = GetDC(NULL);

    // select and realize our palette

    hPal = SelectPalette(hDC, hPal, FALSE);
    RealizePalette(hDC);

    /

    hDIB = GlobalAlloc(GHND, dwLen);

    

    if (!hDIB)
    {
      

      SelectPalette(hDC, hPal, TRUE);
      RealizePalette(hDC);
      ReleaseDC(NULL, hDC);
      return NULL;
    }

   

    lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

    /// use our bitmap info. to fill BITMAPINFOHEADER

    *lpbi = bi;

    //
    GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, NULL, (LPBITMAPINFO)lpbi,
        DIB_RGB_COLORS);


    bi = *lpbi;
    GlobalUnlock(hDIB);

   
    if (bi.biSizeImage == 0)
        bi.biSizeImage = WIDTHBYTES((DWORD)bm.bmWidth * biBits) * bm.bmHeight;

    //

    dwLen = bi.biSize + bi.biSizeImage;

    if (h = GlobalReAlloc(hDIB, dwLen, 0))
        hDIB = h;
    else
    {
        

        GlobalFree(hDIB);
        hDIB = NULL;
        SelectPalette(hDC, hPal, TRUE);
        RealizePalette(hDC);
        ReleaseDC(NULL, hDC);
        return NULL;
    }

    // lock memory block and get pointer to it */

    lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);

    

    if (GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, (LPSTR)lpbi +
            (WORD)lpbi->biSize , (LPBITMAPINFO)lpbi,
            DIB_RGB_COLORS) == 0)
    {
        // clean up and return NULL

        GlobalUnlock(hDIB);
        hDIB = NULL;
        SelectPalette(hDC, hPal, TRUE);
        RealizePalette(hDC);
        ReleaseDC(NULL, hDC);
        return NULL;
    }

    bi = *lpbi;

    // clean up 
    GlobalUnlock(hDIB);
    SelectPalette(hDC, hPal, TRUE);
    RealizePalette(hDC);
    ReleaseDC(NULL, hDC);

    // return handle to the DIB
    return hDIB;
}



i hope you will understand this.. this code will :)compile after some changes.. as i have taken this from a big part of the code for you.
enjoy and save the DIB.
Last edited on
Hello there....
thank you very much....
This code is SDK code .... wright?

well it surely help understand the logic...
but what , i really needed was in plain C...
but thanks anyways...

Well tried to find it on net but didn't got one...
i guess i'll have spend sometime doing by myself...

But guys , please if you have it do let me know please...

1. i thought you may be working on windows.. :(
2. if you can understand the above code by debugging then it will be easy for you to implement it in c.
3. but it will be easy if you use some third party library rather than writing everything from scratch..

which platform you are working on?
Iam working on linux(Fedora8)..

well, i found one library, and I'm using it...
Thank you..
Topic archived. No new replies allowed.