I have the following code:

#include "stdafx.h"
    #include <windows.h>
    #include <GdiPlus.h>
    #pragma comment( lib, "gdiplus" )
     
    int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);
    void gdiscreen();
     
    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"C:\\screen.jpeg", &clsid);
     
    SelectObject(memdc, hOldBitmap);
     
    DeleteObject(memdc);
     
    DeleteObject(membit);
     
    ::ReleaseDC(0,scrdc);
    }
     
    GdiplusShutdown(gdiplusToken);
    }
     
    int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
    {
    using namespace Gdiplus;
    UINT num = 0; // number of image encoders
    UINT size = 0; // size of the image encoder array in bytes
     
    ImageCodecInfo* pImageCodecInfo = NULL;
     
    GetImageEncodersSize(&num, &size);
    if(size == 0)
    return -1; // Failure
     
    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
    if(pImageCodecInfo == NULL)
    return -1; // Failure
     
    GetImageEncoders(num, size, pImageCodecInfo);
     
    for(UINT j = 0; j < num; ++j)
    {
    if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
    {
    *pClsid = pImageCodecInfo[j].Clsid;
    free(pImageCodecInfo);
    return j; // Success
    }
    }
     
    free(pImageCodecInfo);
    return 0;
    }
     
    int main()
    {
    gdiscreen();
    }

And I am trying for about 2+hours to change line 30 to:

string drive = "C:\\"
string dir_save = drive+"screen.jpeg"
bitmap.Save(dir_save, &clsid);

But with no luck...
I have searched tons of forums and sites but many of them where outdated and others simply, didn't work :(

I wish someone can help me! :)

bitmap.Save() does not take std::string as its first parameter. Did you try bitmap.Save(dir_save.c_str(), &clsid); Here is how to convert char* to wchar_t*

Edited 5 Years Ago by Ancient Dragon: n/a

Thanks a lot both of you! Finally I done it!

string str = "C:\\screen.jpg";
wstring str2(search.length(), L' ');
copy(str.begin(), str.end(), str2.begin());

But I am not sure if it's "corrent", the final post here
made me a bit unsure and I can't compile this:

std::copy(str.begin(), str.end(), std::back_inserter(str2));

What do you guys think?

Edited 5 Years Ago by SpecialForce: n/a

This article has been dead for over six months. Start a new discussion instead.