Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failed to capture windows - DeleteObject HBITMAP(0x5d050727) failed - Process not DPI aware #194

Closed
louis030195 opened this issue Feb 17, 2025 · 6 comments · Fixed by mediar-ai/screenpipe#1411 or #195

Comments

@louis030195
Copy link
Contributor

louis030195 commented Feb 17, 2025

some of my users get this error

log::error!("DeleteObject {:?} failed", val);

Process not DPI aware

claude says:

Let me help analyze this Windows GDI code and the DPI awareness issue. Let's think through this:

  1. The error occurs when deleting a bitmap object and seems related to DPI awareness
  2. Windows scales UI elements based on DPI settings, which can cause issues if the application isn't properly DPI aware
  3. The code is using GDI functions for screen capture and needs to handle DPI scaling correctly

Here's how we can fix this:

// ... existing code ...

// Add this at the start of the file after the imports
use windows::Win32::UI::HiDpi::{SetProcessDpiAwareness, PROCESS_DPI_AWARENESS};

// Add this function
pub fn init_dpi_awareness() -> XCapResult<()> {
    unsafe {
        SetProcessDpiAwareness(PROCESS_DPI_AWARENESS::PROCESS_PER_MONITOR_DPI_AWARE)?;
    }
    Ok(())
}

// In the capture_window function, modify the scale factor handling:
pub fn capture_window(
    hwnd: HWND, 
    scale_factor: f32,
    window_info: &WINDOWINFO,
) -> XCapResult<RgbaImage> {
    unsafe {
        // ... existing code ...

        // Update how we handle the scaling
        width = (width as f32 * scale_factor).round() as i32;  // Use round instead of ceil
        height = (height as f32 * scale_factor).round() as i32;

        // ... rest of existing code ...
    }
}

The changes:

  1. Added DPI awareness initialization that should be called when your application starts
  2. Changed ceil() to round() for more accurate scaling calculations
  3. The bitmap deletion error should be resolved once the application is properly DPI aware

You'll need to call init_dpi_awareness() early in your application startup, before any window or screen capture operations. This ensures Windows knows how to handle DPI scaling for your application.

The error you're seeing ("Process not DPI aware") indicates that Windows is trying to handle DPI scaling automatically, which can cause issues with GDI operations. By making the process DPI aware, we tell Windows that we'll handle the scaling ourselves.

@nashaofu
Copy link
Owner

Couldn't replicate the problem.

@louis030195
Copy link
Contributor Author

Image

this is new, did not have until we moved from

xcap = "0.0.12"

to

xcap = { package = "xcap", git = "https://github.com/nashaofu/xcap", rev = "214cb44" }

cc @ologbonowiwi

i use windows through https://parsec.app/

@louis030195
Copy link
Contributor Author

@ologbonowiwi
Copy link
Contributor

so it broke here a6f508a#diff-7407bb83d66acb353fa8805d9df99c53215ad7da7bf40ee2a0fef6e3ab56311b

I changed screenpipe-vision/Cargo.toml to

xcap = "0.2.2"

and the DeleteObject error is gone, although I still see the Process not DPI aware info log

Image

@sorry77
Copy link

sorry77 commented Feb 20, 2025

请问这里的if判断是否忘记取反了?捕获正常也每次都报

                if DeleteObject(val.into()).as_bool() {
                    log::error!("DeleteObject {:?} failed", val);
                }

@ologbonowiwi
Copy link
Contributor

you are totally right @sorry77, nice catch.

opened #195, inverting the condition. it works perfectly now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants