L
L
Limons2021-11-06 21:06:17
C++ / C#
Limons, 2021-11-06 21:06:17

How to save photo from bitmap?

What's wrong, just already tired.

#include <windows.h>
//#include <stdio.h>
#include <iostream>

#include <windef.h>

#include <gdiplus.h>

using namespace std;

#pragma comment(lib, "GdiPlus.lib") 
using namespace Gdiplus;

static const GUID png =
{ 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e } };

HWND hWnd = 0;
void screenShot(HWND hWnd);

void main() {
    HWND hWnd = FindWindowA("57558", 0);
    screenShot(hWnd);
}

void screenShot(HWND hWnd)
{
    HDC scrdc, memdc;
    HBITMAP membit;

    scrdc = GetDC(hWnd);

    RECT sizeWindow;
    GetWindowRect(hWnd, &sizeWindow);
    RECT test;
    cout << sizeWindow.left << " " << sizeWindow.top << " " << sizeWindow.right << " " << sizeWindow.bottom << endl;
    int w = sizeWindow.right - sizeWindow.left;
    int h = sizeWindow.bottom - sizeWindow.top - 16;

    memdc = CreateCompatibleDC(scrdc);
    membit = CreateCompatibleBitmap(scrdc, w, h);
    SelectObject(memdc, membit);
    BitBlt(memdc, 0, 0, w, h, scrdc, 0, 0, SRCCOPY);
    HBITMAP hBitmap = membit;
    Gdiplus::Bitmap bitmap(hBitmap, NULL);

    bitmap.Save(L"C:\\screen.png", &png);
    DeleteObject(hBitmap);
}


If you still don’t find an error, then I’ll explain the essence:
I’m trying to save a screenshot of a window via hwnd to drive C.
Can I use your code instead of mine

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Limons, 2021-11-07
@Limons

In general, I abandoned that code and just wrote a new one.
I am almost 100% sure that the error was in the wrong use of Gdiplus -- GDI+ of the new generation, you can google it if you are interested, to understand in more detail.
Here is the code that captures the entire screen:

#include <windows.h>
#include <stdio.h>
#include <iostream>

#include <gdiplus.h>

using namespace std;

#pragma comment(lib, "GdiPlus.lib") 
using namespace Gdiplus; 


static const GUID png =
{ 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e } };


int main()
{
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    HDC scrdc, memdc;
    HBITMAP membit;

    scrdc = GetDC(0);
    int Height, Width;
    Height = GetSystemMetrics(SM_CYSCREEN);
    Width = GetSystemMetrics(SM_CXSCREEN);

    memdc = CreateCompatibleDC(scrdc);
    membit = CreateCompatibleBitmap(scrdc, Width, Height);
    SelectObject(memdc, membit);

    BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
    HBITMAP hBitmap;
    hBitmap = (HBITMAP)SelectObject(memdc, membit);
    Gdiplus::Bitmap bitmap(hBitmap, NULL);
    bitmap.Save(L"c:\screen.png", &png);

    DeleteObject(hBitmap);
    //GdiplusShutdown(gdiplusToken); не моуг понять, но происходит нарушение прав доступа. Вероятно  gdiplusToken удаляется какой-то функцией, самоуничтожается в плане.
    return 0;
}

Here is a code that only takes a picture of an application window by HWND:
#include <windows.h>
#include <stdio.h>
#include <iostream>

#include <gdiplus.h>

using namespace std;

#pragma comment(lib, "GdiPlus.lib") 
using namespace Gdiplus; 


static const GUID png =
{ 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e } };

void screenShote(HWND hWnd);

int main()
{
    HWND hWnd = FindWindowA("Класс", "Заголовок"); // можно поставить 0 вместо заголвка или класса окна, тогда будет выполнен поиск только по одному из аргументов
    screenShote(hWnd);
    return 0;
}

void screenShote(HWND hWnd) {
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    HDC scrdc, memdc;
    HBITMAP membit;

    scrdc = GetDCEx(hWnd, 0, DCX_WINDOW);
    int Height, Width;
    RECT sizeWindow;
    GetWindowRect(hWnd, &sizeWindow);

    Height = sizeWindow.bottom - sizeWindow.top;
    Width = sizeWindow.right - sizeWindow.left;

    memdc = CreateCompatibleDC(scrdc);
    membit = CreateCompatibleBitmap(scrdc, Width, Height);
    SelectObject(memdc, membit);

    BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
    HBITMAP hBitmap;
    hBitmap = (HBITMAP)SelectObject(memdc, membit);
    Gdiplus::Bitmap bitmap(hBitmap, NULL);
    bitmap.Save(L"c:\\screen.png", &png);

    DeleteObject(hBitmap);
    //GdiplusShutdown(gdiplusToken); не моуг понять, но происходит нарушение прав доступа. Вероятно  gdiplusToken удаляется какой-то функцией, самоуничтожается в плане.
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question