I'm making a code to make a screenshot and save it in a JPEG file type. I found this piece of code but I don't understand why it gives me an error when I remove curly braces after
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
.
Full code:
void gdiscreen()
{
using namespace Gdiplus;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
{
HDC scrdc, memdc;
HBITMAP membit;
scrdc = ::GetDC(0);
int Height = GetSystemMetrics(SM_CYSCREEN);
int Width = GetSystemMetrics(SM_CXSCREEN);
memdc = CreateCompatibleDC(scrdc);
membit = CreateCompatibleBitmap(scrdc, Width, Height);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(memdc, membit);
BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
Gdiplus::Bitmap bitmap(membit, NULL);
CLSID clsid;
GetEncoderClsid(L"image/jpeg", &clsid);
bitmap.Save(L"screen.jpeg", &clsid);
SelectObject(memdc, hOldBitmap);
DeleteObject(memdc);
DeleteObject(membit);
::ReleaseDC(0, scrdc);
}
GdiplusShutdown(gdiplusToken);
}
Can somebody explain to me why I need the curly braces?
And when I remove the curly braces it gives me the following error:
Exception produced in 0x661AF6B8 (GdiPlus.dll) in DebugScreenShotModule.exe: 0xC0000005: Access violation when reading location 0x029E12AC.
You have a variable Gdiplus::Bitmap bitmap
declared within the curly braces. It will be destroyed at the closing }
. Without the curly braces, it won't be destroyed until after GdiplusShutdown
is called.
User contributions licensed under CC BY-SA 3.0