Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is a problem, there will be insufficient memory. #24

Open
m0n1b opened this issue Apr 5, 2024 · 0 comments
Open

There is a problem, there will be insufficient memory. #24

m0n1b opened this issue Apr 5, 2024 · 0 comments

Comments

@m0n1b
Copy link

m0n1b commented Apr 5, 2024

due to not free the DG on the function BitmapToJpg
fix it:

oid BitmapToJpg(HDC* hDc, HBITMAP* hbmpImage, int width, int height)
{
	ULONG_PTR gdiplusToken;
	GdiplusStartupInput gdiplusStartupInput;
	if (GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) != Ok) {
		return;
	}
	Funcs::pSelectObject(*hDc, hbmpImage);
	Funcs::pBitBlt(*hDc, 0, 0, width, height, GetDC(0), 0, 0, SRCCOPY);
	
	IStream* jpegStream = NULL;
	if (CreateStreamOnHGlobal(NULL, TRUE, &jpegStream) != S_OK) {
		GdiplusShutdown(gdiplusToken);
		return;
	}


	Bitmap* image = Bitmap::FromHBITMAP(*hbmpImage, NULL);
	if (!image || image->GetLastStatus() != Ok) {
		jpegStream->Release(); 
		GdiplusShutdown(gdiplusToken); 
		delete image; 
		return;
	}


	if (image->Save(jpegStream, &jpegID, NULL) != Ok) {
		jpegStream->Release(); 
		delete image;
		GdiplusShutdown(gdiplusToken); 
		return;
	}

	delete image; 

	LARGE_INTEGER li = { 0 };
	jpegStream->Seek(li, STREAM_SEEK_SET, NULL);

	Bitmap* jpegImage = Bitmap::FromStream(jpegStream, FALSE);
	jpegStream->Release(); 

	if (!jpegImage || jpegImage->GetLastStatus() != Ok) {
		delete jpegImage; 
		GdiplusShutdown(gdiplusToken); 
		return;
	}

	HBITMAP compressedImage = NULL;
	jpegImage->GetHBITMAP(Color::White, &compressedImage);
	Funcs::pGetDIBits(*hDc, compressedImage, 0, height, g_pixels, (BITMAPINFO*)&g_bmpInfo, DIB_RGB_COLORS);
	delete jpegImage; 

	if (compressedImage) {
		DeleteObject(compressedImage);
	}
	
	GdiplusShutdown(gdiplusToken); 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant