developerLee
developerLee

Reputation: 11

how to remove titlebar in OpenCvSharp, Cv2.ImShow(), c# , wpf

        private void button_Click(object sender, RoutedEventArgs e)
        {
            VideoCapture video = new VideoCapture(0);
            Mat frame = new Mat();

            while (Cv2.WaitKey(33) != 'q') 
            {
                video.Read(frame);
                Cv2.ImShow(" ", frame);
            }

            Cv2.ImWrite("./Capture2.png", frame);

            frame.Dispose();
            video.Release();
            Cv2.DestroyAllWindows();
            
        }
  1. When you press the button, a pop-up window like the image appears.

enter image description here

  1. How to remove the title bar of popups automatically generated by Cv2.ImShow()?

Upvotes: 1

Views: 816

Answers (2)

jakjakjakjakjak
jakjakjakjakjak

Reputation: 114

Just put this before Cv2.ImShow

Cv2.SetWindowProperty(" ", WindowPropertyFlags.Fullscreen, 1);

So your code will be:

private void button_Click(object sender, RoutedEventArgs e)
    {
        VideoCapture video = new VideoCapture(0);
        Mat frame = new Mat();

        while (Cv2.WaitKey(33) != 'q') 
        {
            video.Read(frame);
            Cv2.SetWindowProperty(" ", WindowPropertyFlags.Fullscreen, 1);
            Cv2.ImShow(" ", frame);
        }

        Cv2.ImWrite("./Capture2.png", frame);

        frame.Dispose();
        video.Release();
        Cv2.DestroyAllWindows();
        
    }
 

Upvotes: 0

user2250152
user2250152

Reputation: 20823

Since the version v4.5.1.20210210 OpenCvSharp provides a method that returns a window handle.

You can use Win32 API to remove title bar and show the image.

var windowName = "Image";
using (var source = Cv2.ImRead("<path_to_image>"))
{
    Cv2.NamedWindow(windowName);
    var handle = Cv2.GetWindowHandle(windowName);
    WindowHelper.RemoveTitleBar(handle, windowName);
    Cv2.ImShow(windowName, source);
    Cv2.WaitKey();
    Cv2.DestroyWindow(windowName);
}

WindowHelper:

public static class WindowHelper
{
    const int GWL_STYLE = -16;
    const long WS_OVERLAPPEDWINDOW = 0x00CF0000;
    const long WS_POPUP = 0x80000000;

    public static void RemoveTitleBar(IntPtr hwnd, string windowName)
    {
        // change style of the child HighGui window
        long style = GetWindowLong(hwnd, GWL_STYLE);
        style &= ~WS_OVERLAPPEDWINDOW;
        style |= WS_POPUP;
        SetWindowLong(hwnd, GWL_STYLE, style);

        // change style of the parent HighGui window
        IntPtr hParent = FindWindow(null, windowName);
        style = GetWindowLong(hParent, GWL_STYLE);
        style &= ~WS_OVERLAPPEDWINDOW;
        style |= WS_POPUP;
        SetWindowLong(hParent, GWL_STYLE, style);
    }

    [DllImport("user32", EntryPoint = "GetWindowLongA")]
    public static extern long GetWindowLong(IntPtr hwnd, int nIndex);

    [DllImport("user32", EntryPoint = "SetWindowLongA")]
    public static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);

    [DllImport("user32")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}

Upvotes: 1

Related Questions