Help To Solve Problem. Stream Buffer To Base64 String.

Hi, I have problem with this code:
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
#include <windows.h>
#include <stdio.h>
#include <gdiplus.h>
#include "ScreenCap.h"
#include <wchar.h>
#include "base64.h"
#include <sstream>

using namespace Gdiplus;

int GetEncoderClsid(WCHAR *format, CLSID *pClsid)
{
	unsigned int num = 0,  size = 0;
	GetImageEncodersSize(&num, &size);
	if(size == 0) return -1;
	ImageCodecInfo *pImageCodecInfo = (ImageCodecInfo *)(malloc(size));
	if(pImageCodecInfo == NULL) return -1;
	GetImageEncoders(num, size, pImageCodecInfo);
	for(unsigned int j = 0; j < num; ++j){
		if(wcscmp(pImageCodecInfo[j].MimeType, format) == 0){
			*pClsid = pImageCodecInfo[j].Clsid;
			free(pImageCodecInfo);
			return j;
		}    
	}
	free(pImageCodecInfo);
	return -1;
}

std::string GetScreeny(ULONG uQuality)
{
	//this work ---->
    ULONG_PTR gdiplusToken;
	GdiplusStartupInput gdiplusStartupInput;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	
	HDC hdcScreen  = CreateDC("DISPLAY", NULL, NULL, NULL);
	HDC hdcCapture = CreateCompatibleDC(hdcScreen);
	int nWidth     = GetDeviceCaps(hdcScreen, HORZRES),
	    nHeight    = GetDeviceCaps(hdcScreen, VERTRES),
	    nBPP       = GetDeviceCaps(hdcScreen, BITSPIXEL);
	
	LPBYTE lpCapture;
	BITMAPINFO bmiCapture = { {
		sizeof(BITMAPINFOHEADER), nWidth, -nHeight, 1, nBPP, BI_RGB, 0, 0, 0, 0, 0,
	} };
	HBITMAP hbmCapture = CreateDIBSection(hdcScreen, &bmiCapture,
		DIB_PAL_COLORS, (LPVOID *)&lpCapture, NULL, 0);
	if(!hbmCapture){
		DeleteDC(hdcCapture);
		DeleteDC(hdcScreen);
		GdiplusShutdown(gdiplusToken);
	}
	
	int nCapture = SaveDC(hdcCapture);
	SelectObject(hdcCapture, hbmCapture);
	BitBlt(hdcCapture, 0, 0, nWidth, nHeight, hdcScreen, 0, 0, SRCCOPY);
	RestoreDC(hdcCapture, nCapture);
	DeleteDC(hdcCapture);
	DeleteDC(hdcScreen);
	
	CLSID imageCLSID;
	Bitmap *pScreenShot = new Bitmap(hbmCapture, (HPALETTE)NULL);
	EncoderParameters encoderParams;
	encoderParams.Count = 1;
	encoderParams.Parameter[0].NumberOfValues = 1;
	encoderParams.Parameter[0].Guid  = EncoderQuality;
	encoderParams.Parameter[0].Type  = EncoderParameterValueTypeLong;
	encoderParams.Parameter[0].Value = &uQuality;

	GetEncoderClsid(L"image/jpeg", &imageCLSID);
	//<----- until here

	IStream *pStream = NULL;
    LARGE_INTEGER liZero = {};
    ULARGE_INTEGER pos = {};
    STATSTG stg = {};
    ULONG bytesRead=0;
    HRESULT hrRet=S_OK;

    BYTE* buffer = NULL;  // this is your buffer that will hold the jpeg bytes
    DWORD dwBufferSize = 0;  // this is the size of that buffer;


    hrRet = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
    hrRet = pScreenShot->Save(pStream, &imageCLSID, &encoderParams) == 0 ? S_OK : E_FAIL;
    hrRet = pStream->Seek(liZero, STREAM_SEEK_SET, &pos);
    hrRet = pStream->Stat(&stg, STATFLAG_NONAME);

    // allocate a byte buffer big enough to hold the jpeg stream in memory
    buffer = new BYTE[stg.cbSize.QuadPart];
    hrRet = (buffer == NULL) ? E_OUTOFMEMORY : S_OK;
    dwBufferSize = stg.cbSize.QuadPart;

    // copy the stream into memory
    hrRet = pStream->Read(buffer, stg.cbSize.QuadPart, &bytesRead);

    // now go save "buffer" and "dwBufferSize" off somewhere.  This is the jpeg buffer
    // don't forget to free it when you are done

    // After success or if any of the above calls fail, don't forget to release the stream
	std::string encodedstring = base64_encode(buffer, dwBufferSize);
    if (pStream)
    {
        pStream->Release();
    }

	delete pScreenShot;
	DeleteObject(hbmCapture);
	GdiplusShutdown(gdiplusToken);
	return encodedstring;
}

This function return me a long series of Y ended with other chars but not the base64 string.
I think the problem is std::string encodedstring = base64_encode(buffer, dwBufferSize); but I can't figure out the problem. Any help is appreciated.
The base64 function is: http://www.adp-gmbh.ch/cpp/common/base64.html
Did you tried to write buffer content to a file and see if it produces a valid image ? (using dwBufferSize as argument to WriteFile(), of course).

If NOT, then seek for the problem somewhere else.
ok, I figure out the problem. The buffer save the RGB value of the JPEG. Replacing
 
std::string encodedstring = base64_encode(buffer, dwBufferSize);

with:
1
2
3
4
5
6
	fopen_s (&pFile, "C:\\data.txt","w");
		for(DWORD i=0; i< dwBufferSize; i++){
			//printf(" %d", buffer[i]);
			fprintf(pFile, " %d", buffer[i]);
		}
		fclose(pFile);

I see all the RGB values in the data.txt file. I don' t need the RGB file, I need the base64 string of the JPEG bytes. Suggestions?
Topic archived. No new replies allowed.