c++ - C++ Windows显示无框图像

标签 c++ windows image

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

How to display an image in full screen borderless window in openCV

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

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

此外,图片顶部似乎也有一个1px宽的灰色条。
对于我的应用程序,非常重要的是,每个像素都必须具有应有的值,并且不遗漏任何像素
(或用灰色条覆盖)。图像不得以任何方式失真。

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

这是我的代码,我正在Windows 10上使用VS2019:

#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 window\n");
    }

    // 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;
}

最佳答案

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

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

关于c++ - C++ Windows显示无框图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59674274/

相关文章:

php - 如何向 PNG 添加额外的元数据?

c++ - 从 matlab 生成 C++ 代码

c++ - 为什么我的 dev-c++ "can' t 打开输出文件”?

c# - 使用 System.Net.WebSockets 处理多个子协议(protocol)

c++ - cpp - SDL_JOYHATMOTION 在使用 Windows 时有何不同?

swift - 将屏幕截图保存到 CoreData

c++ - C++ 中的简单复制构造函数

c++ - WinAPI:正确地将 HBITMAP 复制到剪贴板

python - 如何启动 IPython 内核并使用 ZMQ 套接字进行连接?

wpf - 如何在 WPF 图像中加载非常大的源图像?