Skip to content

Instantly share code, notes, and snippets.

@prashanthrajagopal
Last active October 23, 2024 13:14
Show Gist options
  • Save prashanthrajagopal/05f8ad157ece964d8c4d to your computer and use it in GitHub Desktop.
Save prashanthrajagopal/05f8ad157ece964d8c4d to your computer and use it in GitHub Desktop.

Revisions

  1. prashanthrajagopal revised this gist Jun 18, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions screenshot.cpp
    Original file line number Diff line number Diff line change
    @@ -71,13 +71,13 @@ void gdiscreen() {

    int main()
    {
    clock_t t1 = clock();
    clock_t t1 = clock();
    int i;
    int iterations = 10;
    for(i=0;i<iterations;i++){
    gdiscreen();
    }
    clock_t t2 = clock();
    printf("%d iterations: %0.0f fps\n", iterations, iterations/((double)(t2 - t1) / CLOCKS_PER_SEC));
    return 0;
    return 0;
    }
  2. prashanthrajagopal revised this gist Jun 18, 2014. 1 changed file with 13 additions and 9 deletions.
    22 changes: 13 additions & 9 deletions screenshot.cpp
    Original file line number Diff line number Diff line change
    @@ -34,9 +34,8 @@ int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {

    void gdiscreen() {
    using namespace Gdiplus;
    LPSTREAM lpstream;
    // HRESULT res;
    // res = CreateStreamOnHGlobal(NULL, true, &lpstream);
    IStream* istream;
    HRESULT res = CreateStreamOnHGlobal(NULL, true, &istream);
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    @@ -54,9 +53,15 @@ void gdiscreen() {
    Gdiplus::Bitmap bitmap(membit, NULL);
    CLSID clsid;
    GetEncoderClsid(L"image/jpeg", &clsid);
    // bitmap.Save(L"screen.jpeg", &clsid, NULL);
    bitmap.Save(lpstream, &clsid, NULL);

    // bitmap.Save(L"screen.jpeg", &clsid, NULL); // To save the jpeg to a file
    bitmap.Save(istream, &clsid, NULL);

    // Create a bitmap from the stream and save it to make sure the stream has the image
    // Gdiplus::Bitmap bmp(istream, NULL);
    // bmp.Save(L"t1est.jpeg", &clsid, NULL);
    // END

    delete &clsid;
    DeleteObject(memdc);
    DeleteObject(membit);
    ::ReleaseDC(0,scrdc);
    @@ -66,10 +71,9 @@ void gdiscreen() {

    int main()
    {
    LPSTREAM jStream;
    clock_t t1 = clock();
    clock_t t1 = clock();
    int i;
    int iterations = 100;
    int iterations = 10;
    for(i=0;i<iterations;i++){
    gdiscreen();
    }
  3. prashanthrajagopal revised this gist Jun 18, 2014. 1 changed file with 20 additions and 9 deletions.
    29 changes: 20 additions & 9 deletions screenshot.cpp
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    #include <stdio.h>
    #include <windows.h>
    #include <gdiplus/gdiplus.h>
    #include <gdiplus.h>
    #include <time.h>

    int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
    {
    int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {
    using namespace Gdiplus;
    UINT num = 0;
    UINT size = 0;
    @@ -32,9 +32,11 @@ int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
    return 0;
    }

    void gdiscreen()
    {
    void gdiscreen() {
    using namespace Gdiplus;
    LPSTREAM lpstream;
    // HRESULT res;
    // res = CreateStreamOnHGlobal(NULL, true, &lpstream);
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    @@ -48,12 +50,12 @@ void gdiscreen()
    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, NULL);
    // bitmap.Save(L"screen.jpeg", &clsid, NULL);
    bitmap.Save(lpstream, &clsid, NULL);

    DeleteObject(memdc);
    DeleteObject(membit);
    @@ -64,5 +66,14 @@ void gdiscreen()

    int main()
    {
    gdiscreen();
    LPSTREAM jStream;
    clock_t t1 = clock();
    int i;
    int iterations = 100;
    for(i=0;i<iterations;i++){
    gdiscreen();
    }
    clock_t t2 = clock();
    printf("%d iterations: %0.0f fps\n", iterations, iterations/((double)(t2 - t1) / CLOCKS_PER_SEC));
    return 0;
    }
  4. prashanthrajagopal revised this gist Jun 18, 2014. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions screenshot.cpp
    Original file line number Diff line number Diff line change
    @@ -64,8 +64,5 @@ void gdiscreen()

    int main()
    {
    int i;
    for(i=0;i<1000;i++){
    gdiscreen();
    }
    gdiscreen();
    }
  5. prashanthrajagopal revised this gist Jun 18, 2014. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions screenshot.cpp
    Original file line number Diff line number Diff line change
    @@ -52,7 +52,8 @@ void gdiscreen()
    Gdiplus::Bitmap bitmap(membit, NULL);
    CLSID clsid;
    GetEncoderClsid(L"image/jpeg", &clsid);
    bitmap.Save(L"screen.jpeg", &clsid, NULL);

    // bitmap.Save(L"screen.jpeg", &clsid, NULL);

    DeleteObject(memdc);
    DeleteObject(membit);
    @@ -63,5 +64,8 @@ void gdiscreen()

    int main()
    {
    gdiscreen();
    int i;
    for(i=0;i<1000;i++){
    gdiscreen();
    }
    }
  6. prashanthrajagopal revised this gist Jun 17, 2014. 1 changed file with 55 additions and 56 deletions.
    111 changes: 55 additions & 56 deletions screenshot.cpp
    Original file line number Diff line number Diff line change
    @@ -1,68 +1,67 @@
    #include <stdio.h>
    #include <windows.h>
    #include <jpeglib.h>
    #include <gdiplus/gdiplus.h>

    int jpegCapture(char* filename, int quality){
    HBITMAP hBMP;
    HWND desktopWnd;
    int width;
    int height;
    RECT rc;
    HDC hDC;
    HDC hDCmem;

    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE * outfile;
    JSAMPLE* scanline;
    COLORREF pixel;
    int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
    {
    using namespace Gdiplus;
    UINT num = 0;
    UINT size = 0;

    ImageCodecInfo* pImageCodecInfo = NULL;

    GetImageEncodersSize(&num, &size);
    if(size == 0)
    return -1;

    desktopWnd = GetDesktopWindow();
    GetWindowRect(desktopWnd, &rc);
    width = rc.right - rc.left;
    height = rc.bottom - rc.top;
    hDC = GetDC(desktopWnd);
    hDCmem = CreateCompatibleDC(hDC);
    hBMP = CreateCompatibleBitmap(hDC, width, height);
    if(hBMP == NULL) return -2;
    SelectObject(hDCmem, hBMP);
    BitBlt(hDCmem, 0, 0, width, height, hDC, rc.left, rc.top, SRCCOPY);

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    outfile = fopen(filename, "wb");
    jpeg_stdio_dest(&cinfo, outfile);
    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;
    jpeg_set_defaults(&cinfo);
    if(quality < 0) quality = 0;
    if(quality > 100) quality = 100;
    jpeg_set_quality(&cinfo, quality, FALSE);
    jpeg_start_compress(&cinfo, TRUE);
    scanline = new JSAMPLE[width*3];
    for(int posy = 0; posy < height; posy++)
    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
    if(pImageCodecInfo == NULL)
    return -1;

    GetImageEncoders(num, size, pImageCodecInfo);
    for(UINT j = 0; j < num; ++j)
    {
    for(int posx = 0; posx < width; posx++)
    if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
    {
    pixel = GetPixel(hDCmem, posx, posy);
    scanline[posx*3+0] = GetRValue(pixel);
    scanline[posx*3+1] = GetGValue(pixel);
    scanline[posx*3+2] = GetBValue(pixel);
    }
    jpeg_write_scanlines(&cinfo, &scanline, 1);
    *pClsid = pImageCodecInfo[j].Clsid;
    free(pImageCodecInfo);
    return j;
    }
    }
    free(pImageCodecInfo);
    return 0;
    }

    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, NULL);

    DeleteObject(memdc);
    DeleteObject(membit);
    ::ReleaseDC(0,scrdc);
    }
    jpeg_finish_compress(&cinfo);

    jpeg_destroy_compress(&cinfo);
    delete scanline;
    fclose(outfile);
    DeleteDC(hDCmem);
    ReleaseDC(desktopWnd, hDC);
    GdiplusShutdown(gdiplusToken);
    }

    int main()
    {
    jpegCapture("q.jpeg", 30);
    gdiscreen();
    }
  7. prashanthrajagopal created this gist Jun 16, 2014.
    68 changes: 68 additions & 0 deletions screenshot.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #include <stdio.h>
    #include <windows.h>
    #include <jpeglib.h>

    int jpegCapture(char* filename, int quality){
    HBITMAP hBMP;
    HWND desktopWnd;
    int width;
    int height;
    RECT rc;
    HDC hDC;
    HDC hDCmem;

    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE * outfile;
    JSAMPLE* scanline;
    COLORREF pixel;

    desktopWnd = GetDesktopWindow();
    GetWindowRect(desktopWnd, &rc);
    width = rc.right - rc.left;
    height = rc.bottom - rc.top;
    hDC = GetDC(desktopWnd);
    hDCmem = CreateCompatibleDC(hDC);
    hBMP = CreateCompatibleBitmap(hDC, width, height);
    if(hBMP == NULL) return -2;
    SelectObject(hDCmem, hBMP);
    BitBlt(hDCmem, 0, 0, width, height, hDC, rc.left, rc.top, SRCCOPY);

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    outfile = fopen(filename, "wb");
    jpeg_stdio_dest(&cinfo, outfile);
    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;
    jpeg_set_defaults(&cinfo);
    if(quality < 0) quality = 0;
    if(quality > 100) quality = 100;
    jpeg_set_quality(&cinfo, quality, FALSE);
    jpeg_start_compress(&cinfo, TRUE);
    scanline = new JSAMPLE[width*3];
    for(int posy = 0; posy < height; posy++)
    {
    for(int posx = 0; posx < width; posx++)
    {
    pixel = GetPixel(hDCmem, posx, posy);
    scanline[posx*3+0] = GetRValue(pixel);
    scanline[posx*3+1] = GetGValue(pixel);
    scanline[posx*3+2] = GetBValue(pixel);
    }
    jpeg_write_scanlines(&cinfo, &scanline, 1);
    }
    jpeg_finish_compress(&cinfo);

    jpeg_destroy_compress(&cinfo);
    delete scanline;
    fclose(outfile);
    DeleteDC(hDCmem);
    ReleaseDC(desktopWnd, hDC);
    }

    int main()
    {
    jpegCapture("q.jpeg", 30);
    }