17 using namespace Gdiplus;
19 GdiplusStartupInput gdiplusStartupInput;
20 GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
25 ImageCodecInfo *pImageCodecInfo = NULL;
27 GetImageEncodersSize(&num, &size);
29 throw std::runtime_error(
"window2file");
31 pImageCodecInfo = (ImageCodecInfo *)(malloc(size));
32 if (pImageCodecInfo == NULL)
33 throw std::runtime_error(
"window2file");
35 GetImageEncoders(num, size, pImageCodecInfo);
36 for (UINT j = 0; j < num; ++j)
38 if (wcscmp(pImageCodecInfo[j].MimeType, L
"image/png") == 0)
40 myPngclsid = pImageCodecInfo[j].Clsid;
41 free(pImageCodecInfo);
46 throw std::runtime_error(
"window2file cannot find encoder");
50 Gdiplus::GdiplusShutdown(gdiplusToken);
57 void save(
gui &w,
const std::string &filename)
61 void save(HWND hw,
const std::string &filename)
65 HDC scrdc = ::GetDC(hw);
67 GetClientRect(hw, &rcClient);
68 int Height = rcClient.bottom - rcClient.top;
69 int Width = rcClient.right - rcClient.left;
70 memdc = CreateCompatibleDC(scrdc);
71 membit = CreateCompatibleBitmap(scrdc, Width, Height);
72 SelectObject(memdc, membit);
73 BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
75 Gdiplus::Bitmap bitmap(membit, NULL);
76 std::wstringstream wss;
77 wss << filename.c_str();
93 bool draw(
gui &w,
const std::string &filename)
96 std::wstringstream wss;
97 wss << filename.c_str();
98 auto bitmap =
new Gdiplus::Bitmap(wss.str().c_str());
101 if( bitmap->GetLastStatus() != S_OK)
104 float xh = bitmap->GetHeight();
105 float xw = bitmap->GetWidth();
110 GetClientRect(w.
handle(), &r);
111 int rw = r.right - r.left;
112 int rh = r.bottom - r.top;
115 if (xh > rh || xw > rw)
126 Gdiplus::PointF dst[] =
128 Gdiplus::PointF(0.0f, 0.0f),
129 Gdiplus::PointF(xw, 0.0f),
130 Gdiplus::PointF(0.0f, xh),
134 Gdiplus::Graphics graphics(GetDC(w.
handle()));
135 graphics.SetCompositingMode(Gdiplus::CompositingModeSourceCopy);
136 auto ret = graphics.DrawImage(bitmap, dst, 3);
140 if( ret != Gdiplus::Status::Ok )
148 ULONG_PTR gdiplusToken;
The base class for all windex gui elements.
Definition: wex.h:824
HWND handle()
get window handle
Definition: wex.h:1655
transfer image between window contents and image file in PNG format.
Definition: window2file.h:13
bool draw(gui &w, const std::string &filename)
Draw png file in window.
Definition: window2file.h:93
void save(gui &w, const std::string &filename)
Save window contents to image file.
Definition: window2file.h:57