sciter-sdk / rust-sciter

Rust bindings for Sciter
https://sciter.com
MIT License
806 stars 76 forks source link

Image::with_data( ) always say "Access violation" #134

Closed Donald2010 closed 2 years ago

Donald2010 commented 2 years ago

I’ve got a pointer hbitmap of type winapi::um::wingdi::BITMAP, and want to convert it to sciter::graphics::Image: let s =std::slice::from_raw_parts(hbitmap as *mut BYTE, (width*height*4) as usize); // convert hbitmap to &[u8] let img = sciter::graphics::Image::with_data((width, height), false, s);

When debugging, it gave error "unhandled exception. 0xC0000005: Access violation" at this line let ok = (_GAPI.imageCreateFromPixmap)(&mut h, width, height, with_alpha as BOOL, pixmap.as_ptr()); in Image::with_data( ) function.

The hbitmap is obtained this way let handle = GetDesktopWindow(); let mut HDC_screen = um::winuser::GetDC(handle); let mut HDC_compatible_DC = um::wingdi::CreateCompatibleDC(HDC_screen); let hbitmap = um::wingdi::CreateCompatibleBitmap(HDC_screen, width, height); let my_bit_blt = um::wingdi::BitBlt(HDC_compatible_DC, 0, 0, width, height, HDC_screen, 0, 0, um::wingdi::SRCCOPY|um::wingdi::CAPTUREBLT);

I 'm ont sure the error is caused by the type conversion of bitmap to &[u8], or the function Image::with_data( ) itself. Do you have any idea ?

pravic commented 2 years ago

Image::with_data expects data in BGRA format. But HBITMAP is a handle, not data. See GetObject and GetDIBits instead.

Check an MSDN example on how to capture a desktop image.

Also, in Sciter.TIS there's View.screenBox(monitorIndex, #snapshot) which gives you a captured Image in script. In Sciter.JS there's Window.this.screenBox("snapshot") with the same purpose, but it gives the current monitor only.

pravic commented 2 years ago

Looks like you've got an answer in forums as well: https://sciter.com/forums/topic/how-to-get-image-from-hbitmap/

Closing for now.