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

[Windows剪贴板句柄占用问题]windows 11 剪切板写入图片报错 #55

Closed
horou-dsk opened this issue Jan 10, 2025 · 9 comments
Closed

Comments

@horou-dsk
Copy link
Contributor

The problem | 问题描述

let d_image = image::load_from_memory_with_format(&image, image::ImageFormat::Png)
    .map_err(|err| tauri::Error::Io(std::io::Error::new(std::io::ErrorKind::Other, err)))?;
let ctx = ClipboardContext::new().unwrap();
if let Err(err) = ctx.set_image(RustImageData::from_dynamic_image(d_image)) {
    eprintln!("set image to clipboard error: {}", err);
}

报错:set png image error, code = OSError(1418): 线程没有打开的剪贴板。

Release version | 版本

0.2.2

Operating system | 操作系统

Windows 11 26100.2605

Logs | 日志

set png image error, code = OSError(1418): 线程没有打开的剪贴板。

@ChurchTao
Copy link
Owner

你是不是系统中运行了其他管理剪贴板的软件

@ChurchTao
Copy link
Owner

Windows 中剪贴板进程只能被一个线程持有,本库的逻辑是,在尝试写入之前,会尝试获得剪贴板句柄,获取到后,尝试写入,如果在这间隔之内,句柄被别人获取走了,或者压根没有获取到,就会报这个错

@horou-dsk
Copy link
Contributor Author

我也不清楚有没有其他的剪切板管理软件在运行,我一般是用windows自带的 windows + v 这个管理剪切板,这个会有影响吗,

我如果写入比较小的图片就能成功,但是写入比较大的就会出现这个错误,这种有没有其他解决方案

@ChurchTao
Copy link
Owner

比较大是有多大?按理说不会出现这种情况,你的测试 demo 能贴一下完整的代码吗?

@horou-dsk
Copy link
Contributor Author

我是做的一个截屏功能,截取一小部分区域写入就可以,截取大概超过1/4屏幕大小就会报错,在4k显示器上

@ChurchTao
Copy link
Owner

@horou-dsk
我刚刚在我的 Win11 机器上测试了,写入 2.5MB 的全屏幕截图也是成功的[多测尝试],我推测应该是你机器有什么另外的的软件导致了这个问题。你可以提供一段 Bug 重现的最小代码片段,然后提供一下图片,我再测试一下看看

@horou-dsk
Copy link
Contributor Author

https://github.com/aamiros/screenshot.git

我单独提取了这一部分功能,在我这边debug模式下是稳定复现,不过release模式下,错误的概率就会小很多,还有就是存储到剪切板图片的颜色有问题

@horou-dsk
Copy link
Contributor Author

我发现是什么原因了,我装了有 uTools 这个软件,这个软件有一个剪切板历史的功能,应该是抢占了剪切板的句柄导致的,我把它关了似乎就没问题了

@ChurchTao
Copy link
Owner

可以的,跟我的猜想一致,我把这个 issue 置顶了,后面有需要的朋友也可以看到

@ChurchTao ChurchTao pinned this issue Jan 15, 2025
@ChurchTao ChurchTao changed the title windows 11 剪切板写入图片报错 [Windows剪贴板句柄占用问题]windows 11 剪切板写入图片报错 Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants