C++ 窗口显示无框图像



我有一个外接显示器,我想显示与外接显示器高度和宽度完全相同的无边框/无框图像。 我从OpenCV开始,但我在获得无边框图像时遇到了问题。 经过一番搜索,我发现了这个问题:

如何在 openCV 的全屏无边框窗口中显示图像

卡尔菲利普的回答有很大的帮助。 但是,我被困在A.k.在他/她对答案的评论中提到的问题:

此方法适用于低于显示器分辨率的图像。 如果我有一个分辨率等于显示器的图像,它会在底部留下一个灰色条。请问如何删除它?

此外,图像顶部似乎还有一个 1px 宽的灰色条。对于我的应用程序,每个像素都具有应有的值并且没有遗漏任何像素非常重要 (或被灰色条覆盖(。图像不得以任何方式扭曲。

我不是在寻找超快速的解决方案,但我打算以大约 10Hz 的频率写入图像。 此外,我只在 Windows 上工作,因此解决方案不必是跨平台的。

这是我的代码,我正在使用VS10的Windows 2019:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <vector>
#include <Windows.h>
int main() {
// Pixels of external monitor, can be different later
size_t N_x = 2560; // 1920
size_t N_y = 1440; // 1080
// My display resolution. Used to shift the OpenCV image
size_t disp_width  = 2560;
size_t disp_height = 1440;
// To verify that the image is written correctly I generate a sawtooth image with a 4 pixel period.
byte period = 4;
byte slope = 110 / (period - 1);
std::vector<byte> image_vec (N_y * N_x);
for (size_t i = 0; i < N_y; i++) {
for (size_t j = 0; j < N_x; j++) {
image_vec.at(i * N_x + j) = slope * (j % period);
}
}
cv::Mat image = cv::Mat(N_y, N_x, CV_8UC1);
memcpy(image.data, image_vec.data(), image_vec.size() * sizeof(byte));
// When I use cv::WINDOW_NORMAL instead of cv::WINDOW_FULLSCREEN the image gets distorted in the horizontal direction
cv::namedWindow("Display Window", cv::WINDOW_FULLSCREEN);
imshow("Display Window", image);
// Grab the image and resize it, code taken from karlphillip's answer
HWND win_handle = FindWindowA(0, "Display Window");
if (!win_handle) {
printf("Could not find windown");
}
// Resize
unsigned int flags = (SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
flags &= ~SWP_NOSIZE;
unsigned int x = 0;
unsigned int y = 0;
unsigned int w = image.cols;
unsigned int h = image.rows;
SetWindowPos(win_handle, HWND_NOTOPMOST, x, y, w, h, flags);
// Borderless
SetWindowLong(win_handle, GWL_STYLE, GetWindowLong(win_handle, GWL_EXSTYLE) | WS_EX_TOPMOST);
ShowWindow(win_handle, SW_SHOW);
cv::waitKey(0);
return EXIT_SUCCESS;
}

您可以在应用程序中显示图像(也可以编写(,并使用ApplicationView类 https://learn.microsoft.com/en-us/uwp/api/Windows.UI.ViewManagement.ApplicationView?redirectedfrom=MSDN 将应用程序设置为全屏模式。示例代码在 https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/FullScreenMode

https://learn.microsoft.com/en-us/uwp/api/windows.ui.viewmanagement.applicationview.tryenterfullscreenmode 此方法基于 UWP,因此在早期版本上使用它时可能会遇到问题

我看了一下 UWP,但这不是我想要的应用程序。最后,我选择使用OpenGL/FreeGLUT在经过一些尝试和错误后运行良好。如果有人感兴趣,我可以写一个更详细的答案。