简体   繁体   English

为什么在这里需要这个花括号? 谁能解释我为什么会这样?

[英]Why do i need this curly braces here? Can anybody explain me why does this happens?

I'm making a code to make a screenshot and save it in a JPEG file type. 我正在编写代码以制作屏幕截图并将其保存为JPEG文件类型。 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); 我找到了这段代码,但是我不明白为什么在GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);之后删除花括号时为什么会给我一个错误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. 您在花括号内声明了一个变量Gdiplus::Bitmap bitmap It will be destroyed at the closing } . 将在结束时销毁} Without the curly braces, it won't be destroyed until after GdiplusShutdown is called. 没有花括号,直到调用GdiplusShutdown 它才被销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM