Converting BMP to JPG Images.

Aug 30, 2016 at 1:55am
Hey Guys!

I have spent the last 48 hours trying to find some code to help me with converting BMP to JPG files. Normally I wouldn't ask for a "Spoon Feed" but I am really stuck with this problem!! The BMP file that is generated from the below code sits around 4MB!! This is the main reason for me wanting to go down the JPG route!

Many Thanks,

Googie!

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
#define WIN32_LEAN_AND_MEAN 
#include<windows.h>
#include<stdio.h>

void ScreenShot(char*BmpName)
{
	HWND DesktopHwnd = GetDesktopWindow();
	RECT DesktopParams;
	HDC DevC = GetDC(DesktopHwnd);
	GetWindowRect(DesktopHwnd,&DesktopParams);
	DWORD Width = DesktopParams.right - DesktopParams.left;
	DWORD Height = DesktopParams.bottom - DesktopParams.top;

	DWORD FileSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+(sizeof(RGBTRIPLE)+1*(Width*Height*4));
	char *BmpFileData = (char*)GlobalAlloc(0x0040,FileSize);

	PBITMAPFILEHEADER BFileHeader = (PBITMAPFILEHEADER)BmpFileData;
	PBITMAPINFOHEADER  BInfoHeader = (PBITMAPINFOHEADER)&BmpFileData[sizeof(BITMAPFILEHEADER)];

	BFileHeader->bfType = 0x4D42; // BM
	BFileHeader->bfSize = sizeof(BITMAPFILEHEADER);
	BFileHeader->bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);

	BInfoHeader->biSize = sizeof(BITMAPINFOHEADER);
	BInfoHeader->biPlanes = 1;
	BInfoHeader->biBitCount = 16;
	BInfoHeader->biCompression = BI_RGB;
	BInfoHeader->biHeight = Height;
	BInfoHeader->biWidth = Width;

	RGBTRIPLE *Image = (RGBTRIPLE*)&BmpFileData[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
	RGBTRIPLE color;
	
	HDC CaptureDC = CreateCompatibleDC(DevC);
	HBITMAP CaptureBitmap = CreateCompatibleBitmap(DevC,Width,Height);
	SelectObject(CaptureDC,CaptureBitmap);
	BitBlt(CaptureDC,0,0,Width,Height,DevC,0,0,SRCCOPY|CAPTUREBLT);
	GetDIBits(CaptureDC,CaptureBitmap,0,Height,Image,(LPBITMAPINFO)BInfoHeader, DIB_RGB_COLORS);

	DWORD Junk;
	HANDLE FH = CreateFileA(BmpName,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,0,0);

	WriteFile(FH,BmpFileData,FileSize,&Junk,0);
	CloseHandle(FH);
        GlobalFree(BmpFileData); 
}

int main(int argc , char *argv[]) {
char bumper[] = "Hello.bmp";
	ScreenShot(bumper);
}
Aug 30, 2016 at 11:07am
Lol beyond my knowledge I never got into this stuff yet.
Ask on General C++ Programming you will get answered by more skilled people than in the Beginners section.
Aug 30, 2016 at 12:41pm
Use a library for that.

E.g.
http://libjpeg.sourceforge.net/
http://www.cimg.eu/

With gdi+ you can use the CImage class:
https://msdn.microsoft.com/en-us/library/bwea7by5.aspx
Topic archived. No new replies allowed.