我有一个显示图形的应用程序,因为结果通常很有趣(由于错误或故意)我不能快速保存屏幕截图。所以我做了一个截图按钮。
我使用了来自wxWidgets forum FAQ的代码但不幸的是,此方法仅保存截图上的图像(也适用于全屏截图)。其他一切都是透明的。
出于某种原因,这只会在 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制作):
答案 0 :(得分:1)
如果图像在BMP中正确显示而不是PNG,则问题可能是由于透明度,即图像的其余部分必须将其Alpha通道设置为wxIMAGE_ALPHA_TRANSPARENT
。如果确实如此,那么使用
wxImage image = bmp.ConvertToImage();
image.ClearAlpha();
image.SaveFile("foo.png", wxBITMAP_TYPE_PNG);
应该有所帮助,但我仍然不知道为什么它首先是透明的。
如果这仍然发生在wxWidgets 3.0(目前RC2可用,最终将在下周),如果你能找到一个简单的再现问题,那么值得将它报告为bug。
答案 1 :(得分:0)
您应该在此行之前初始化位图到staticbitmap
之类的任何对象:
memDC.SelectObject(wxNullBitmap);