Skip to content

Commit

Permalink
release img
Browse files Browse the repository at this point in the history
  • Loading branch information
xland committed Jan 2, 2025
1 parent 205c4fc commit 53cfc13
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 50 deletions.
85 changes: 40 additions & 45 deletions App/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,47 @@ QMouseEvent Util::createMouseEvent(const LPARAM& lParam, const QEvent::Type& typ
return QMouseEvent(type, p, g, btn, btn, mf);
}

QImage Util::printWindow(WinBox* win)
{
HDC hScreen = GetDC(win->hwnd);
HDC hDC = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, win->w, win->h);
DeleteObject(SelectObject(hDC, hBitmap));
auto flag = PrintWindow(win->hwnd, hDC, PW_RENDERFULLCONTENT);
spdlog::info("PrintWindow {}",flag);
auto img = QImage(win->w, win->h, QImage::Format_ARGB32);
BITMAPINFO info = { sizeof(BITMAPINFOHEADER), (long)win->w, 0 - (long)win->h, 1, 32, BI_RGB, (DWORD)win->w * 4 * win->h, 0, 0, 0, 0 };
GetDIBits(hDC, hBitmap, 0, win->h, img.bits(), &info, DIB_RGB_COLORS);
DeleteDC(hDC);
DeleteObject(hBitmap);
ReleaseDC(NULL, hScreen);
return img;
}

QImage Util::printWindow2(WinBox* win)
{
//auto dwmCapture = new DWMCapture();
//auto hr = dwmCapture->Init();
//if (FAILED(hr)) {
// delete dwmCapture;
//}
//BOX* box = nullptr;
//CAPTURE_DATA* data = new CAPTURE_DATA();
//dwmCapture->CaptureWindow(win->hwnd, box, data);



HDC hdcSrc = GetWindowDC(win->hwnd);
HDC hdcDest = CreateCompatibleDC(hdcSrc);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc, win->w, win->h);
auto hOld = SelectObject(hdcDest, hBitmap);
BitBlt(hdcDest, 0, 0, win->w, win->h, hdcSrc, 0, 0, SRCCOPY);

auto img = QImage(win->w, win->h, QImage::Format_ARGB32);
BITMAPINFO info = { sizeof(BITMAPINFOHEADER), (long)win->w, 0 - (long)win->h, 1, 32, BI_RGB, (DWORD)win->w * 4 * win->h, 0, 0, 0, 0 };
GetDIBits(hdcDest, hBitmap, 0, win->h, img.bits(), &info, DIB_RGB_COLORS);
//QImage Util::printWindow(WinBox* win)
//{
// HDC hScreen = GetDC(win->hwnd);
// HDC hDC = CreateCompatibleDC(hScreen);
// HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, win->w, win->h);
// DeleteObject(SelectObject(hDC, hBitmap));
// auto flag = PrintWindow(win->hwnd, hDC, PW_RENDERFULLCONTENT);
// spdlog::info("PrintWindow {}",flag);
// auto img = QImage(win->w, win->h, QImage::Format_ARGB32);
// BITMAPINFO info = { sizeof(BITMAPINFOHEADER), (long)win->w, 0 - (long)win->h, 1, 32, BI_RGB, (DWORD)win->w * 4 * win->h, 0, 0, 0, 0 };
// GetDIBits(hDC, hBitmap, 0, win->h, img.bits(), &info, DIB_RGB_COLORS);
// DeleteDC(hDC);
// DeleteObject(hBitmap);
// ReleaseDC(NULL, hScreen);
// return img;
//}

SelectObject(hdcDest, hOld);
DeleteDC(hdcDest);
DeleteObject(hBitmap);
ReleaseDC(win->hwnd, hdcSrc);
return img;
}
//QImage Util::printWindow2(WinBox* win)
//{
// //auto dwmCapture = new DWMCapture();
// //auto hr = dwmCapture->Init();
// //if (FAILED(hr)) {
// // delete dwmCapture;
// //}
// //BOX* box = nullptr;
// //CAPTURE_DATA* data = new CAPTURE_DATA();
// //dwmCapture->CaptureWindow(win->hwnd, box, data);
// HDC hdcSrc = GetWindowDC(win->hwnd);
// HDC hdcDest = CreateCompatibleDC(hdcSrc);
// HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc, win->w, win->h);
// auto hOld = SelectObject(hdcDest, hBitmap);
// BitBlt(hdcDest, 0, 0, win->w, win->h, hdcSrc, 0, 0, SRCCOPY);
// auto img = QImage(win->w, win->h, QImage::Format_ARGB32);
// BITMAPINFO info = { sizeof(BITMAPINFOHEADER), (long)win->w, 0 - (long)win->h, 1, 32, BI_RGB, (DWORD)win->w * 4 * win->h, 0, 0, 0, 0 };
// GetDIBits(hdcDest, hBitmap, 0, win->h, img.bits(), &info, DIB_RGB_COLORS);
// SelectObject(hdcDest, hOld);
// DeleteDC(hdcDest);
// DeleteObject(hBitmap);
// ReleaseDC(win->hwnd, hdcSrc);
// return img;
//}

QImage Util::printScreen(const int& x, const int& y, const int& w, const int& h)
{
Expand Down
4 changes: 2 additions & 2 deletions App/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Util
public:
static QMouseEvent createMouseEvent(const QEvent::Type& type = QEvent::MouseMove, const Qt::MouseButton& btn = Qt::MouseButton::LeftButton);
static QMouseEvent createMouseEvent(const LPARAM& lParam, const QEvent::Type& type, const Qt::MouseButton& btn = Qt::MouseButton::LeftButton);
static QImage printWindow(WinBox* win);
static QImage printWindow2(WinBox* win);
//static QImage printWindow(WinBox* win);
//static QImage printWindow2(WinBox* win);
static QImage printScreen(const int& x, const int& y, const int& w, const int& h);
static void imgToClipboard(const QImage& img);
static bool posInScreen(const int& x, const int& y);
Expand Down
6 changes: 3 additions & 3 deletions Shape/ShapeMosaicLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ void ShapeMosaicLine::mouseRelease(QMouseEvent* event)
else {
painter.drawImage(QPoint(0, 0), winImg.copy(pathRect.toRect()));
}
mosaicImg = QImage(0,0);
winImg = QImage(0, 0);
mosaicImg = QImage();
winImg = QImage();

if (state == ShapeState::sizing0) {
QPainterPath tempPath;
Expand Down Expand Up @@ -93,7 +93,7 @@ void ShapeMosaicLine::mousePress(QMouseEvent* event)
event->accept();
}
else if (hoverDraggerIndex >= 0) {
imgPatch = QImage(0, 0);
imgPatch = QImage();
createMosaicImg();
pressPos = event->position();
state = (ShapeState)((int)ShapeState::sizing0 + hoverDraggerIndex);
Expand Down

0 comments on commit 53cfc13

Please sign in to comment.