我有一个显示图表的应用程序,并且由于结果通常很有趣(由于错误或故意),我无法快速保存屏幕截图。所以我做了一个截图按钮。
我使用了wxWidgets 论坛常见问题解答中的代码,但不幸的是,此方法仅将图像保存在屏幕截图上(也适用于全屏屏幕截图)。其他一切都是透明的。
出于某种原因,这只发生在PNG图像导出中。导出为 BMP 或 JPG 就可以了。一定有问题:
screenshot.SaveFile("image.png", wxBITMAP_TYPE_PNG);
我在 wxWidgets 中加载了 PNG 处理器:
wxImage::AddHandler(new wxPNGHandler);
代码:
//Create a DC for the main window
wxClientDC dcScreen(GetParent());
//Get the size of the screen/DC
wxCoord screenWidth, screenHeight;
dcScreen.GetSize(&screenWidth, &screenHeight);
//Create a Bitmap that will later on hold the screenshot image
//Note that the Bitmap must have a size big enough to hold the screenshot
//-1 means using the current default colour depth
screenshot.Create(screenWidth, screenHeight,-1);
//Create a memory DC that will be used for actually taking the screenshot
wxMemoryDC memDC;
//Tell the memory DC to use our Bitmap
//all drawing action on the memory DC will go to the Bitmap now
memDC.SelectObject(screenshot);
//Blit (in this case copy) the actual screen on the memory DC
//and thus the Bitmap
memDC.Blit( 0, //Copy to this X coordinate
0, //Copy to this Y coordinate
screenWidth, //Copy this width
screenHeight, //Copy this height
&dcScreen, //From where do we copy?
0, //What's the X offset in the original DC?
0 //What's the Y offset in the original DC?
);
//Select the Bitmap out of the memory DC by selecting a new
//uninitialized Bitmap
memDC.SelectObject(wxNullBitmap);
图片:
而不是(在 Windows 中使用 Alt+PrintSreen 制作):