How to display an image with OpenCV as a Screen Saver on Windows

asked 2014-10-19 06:41:37 -0600

Linus gravatar image

updated 2020-09-28 23:43:45 -0600

I am trying to create a screen saver with OpenCV and the scrnsave library on Windows. To start I have tried to display an image using the cv::imshow function. To do this I load the image from my computer with the cv::imread function, after that I instantiate a window with the name "image" and then get the handler of that window, and set the parent to the handler provided by the scrnsave library.

However, using the code below it will give me an gray window with nothing on it, it shouldn't be decorated with maximum, minimize or exit buttons. I want the actual image to be drawn to the foreground on top of everything with the handler provided by the Windows library, just like drawing a red circle onto a black background.

I didn't find anything on the web explaining how to implement OpenCV routines into my screen saver.

Here is my abbreviated code for my screen saver:

LRESULT WINAPI ScreenSaverProc(HWND hWnd, 
     UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_CREATE:{
             myImage = cv::imread("path/to/my/folder/image.png", CV_LOAD_IMAGE_UNCHANGED);
             cv::namedWindow("image",cv::WINDOW_AUTOSIZE);
             cv::imshow("image",myImage);
             HWND hWnd2 = (HWND) cvGetWindowHandle("image"); 
             ::SetParent(hWnd2, hWnd);
             ::ShowWindow(hWnd, SW_HIDE);
            return 0;
        }
        case WM_DESTROY:{
            cv::destroyWindow("image");
            PostQuitMessage(0);
            return 0;
        }
    }
    return DefScreenSaverProc(hWnd,message,wParam,lParam);
}

Once again it just gave me a empty and gray window, it seems to have created a separate window instead of drawing it onto the foreground.

edit retag flag offensive close merge delete