0

I'm building a module for an application and, this module should take a print from the screen and show it into a QGraphicsView.

This is my Print Screen code that saves the taken photo into a jpg file:

#include <gdiplus.h>

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; 

    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
    if(pImageCodecInfo == NULL)
        return -1;

    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;
        }    
    }
    free(pImageCodecInfo);
    return 0;
}

void gdiscreen() {
    using namespace Gdiplus;
    IStream* istream;
    HRESULT res = CreateStreamOnHGlobal(NULL, true, &istream);
    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); //Type of image
        bitmap.Save(L"print.jpeg", &clsid, NULL); //Name of the file to save
        bitmap.Save(istream, &clsid, NULL);
        
        delete &clsid;
        DeleteObject(memdc);
        DeleteObject(membit);
        ::ReleaseDC(0,scrdc);
    }
    GdiplusShutdown(gdiplusToken);
    istream->Release();
}

int main(){
    gdiscreen();
    return 0;
}

Where on this code could I get the necessary "image code" to run into a QGraphicsView

8
  • 2
    You should do one thing at a time. Start to learn C++ properly, then you can open the Qt door :) By the way, never delete an automatic variable. You must only delete what was newed. Commented Nov 8, 2023 at 13:31
  • I need it for my Graduation Work at School. I choose C++ at the beginning of the year; so I got no choice than keep going. If you could help me I'd be very thankful Commented Nov 8, 2023 at 13:42
  • 3
    I'm sorry for the confusion, I never said you shouldn't keep going. Actually I rather said the opposite, you should keep going, but the right way. C++ is not a language you can learn by try and guess. I'm not saying you should not use C++, I'm saying you should learn it properly to be able to use it. You could make use of some good C++ books for that purpose (or a teacher, or online resources such as learncpp.com, etc...). Commented Nov 8, 2023 at 13:48
  • 1
    You seem to be using HBITMAP, and QImage has a static method: QImage::fromHBITMAP(HBITMAP hbitmap. What about graphicsView.scene()->addPixmap(QPixmap::fromImage(QImage::fromHBITMAP(hbitmap));? Commented Nov 8, 2023 at 17:29
  • 1
    @kuybt6 Adding to what Fareanor said: Qt is an extremely powerful, extended and extensible toolkit. Its learning curve is not consistent: some basic aspects are quite easy to understand at first (leading you to the impression that "it's easy", even as a beginner) but, as you get to know it, you then realize its complexity (and the curve becomes reversed) along with its power. It may take months, if not years to actually master even its basic principles. I don't mean this to scare you away from it, but to carefully consider the implications. Be patient: learning is done by small steps. Commented Nov 9, 2023 at 4:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.