F
F
FlensT2021-09-01 18:52:15
Rust
FlensT, 2021-09-01 18:52:15

How to take a screenshot with winapi using Rust?

At the moment there is such code (not the best, the main task is to make it work):

Code

use winapi::*;
use std::ptr;
use ::core::mem::size_of;

fn get_sct(){
    unsafe{
        let mut HDC_screen = um::winuser::GetDC(ptr::null_mut());
        let mut HDC_compatible_DC = um::wingdi::CreateCompatibleDC(HDC_screen);

        let bmi = um::wingdi::BITMAPINFO {
            bmiHeader: um::wingdi::BITMAPINFOHEADER {
                biSize: size_of::<um::wingdi::BITMAPINFOHEADER>() as shared::minwindef::DWORD,
                biWidth: 1920,
                biHeight: 1080,
                biPlanes: 1,
                biBitCount: 32,
                biCompression: um::wingdi::BI_RGB,
                biSizeImage: 0,
                biXPelsPerMeter: 0,
                biYPelsPerMeter: 0,
                biClrUsed: 0,
                biClrImportant: 0,
            },
            bmiColors: [um::wingdi::RGBQUAD {
                rgbBlue: 0,
                rgbGreen: 0,
                rgbRed: 0,
                rgbReserved: 0
            }],
        };
        let mut my_dib_data: *mut ctypes::c_void = ptr::null_mut();
        let hbitmap: shared::windef::HBITMAP = um::wingdi::CreateDIBSection(HDC_screen, 
                                                                            &bmi, 
                                                                            um::wingdi::DIB_RGB_COLORS, 
                                                                            &mut my_dib_data,
                                                                            ptr::null_mut(),
                                                                            0);

        um::wingdi::SelectObject(HDC_compatible_DC, hbitmap as shared::windef::HGDIOBJ);
        let my_bit_blt: shared::minwindef::BOOL = um::wingdi::BitBlt(HDC_compatible_DC, 
                                                                    0, 
                                                                    0, 
                                                                    1920, 
                                                                    1080, 
                                                                    HDC_screen, 
                                                                    0, 
                                                                    0,
                                                                    um::wingdi::SRCCOPY);
        println!("{}", my_bit_blt);
    }
}


BitBlt outputs "True", i.e. pixels are copied, but I don't understand what I need to do next.
Tell me how I need to proceed further in order to save and / or simply get the necessary information for further translation of "this" into the "Mat" object of the opencv library.
The main task is to create an opencv "Mat" object from this, further work with the image obtained by this method.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question