WPF:使用VB.NET(不使用GDI)获取像素
我正在开发一个 WPF 应用程序,我希望检索屏幕上像素的颜色值(屏幕是我的应用程序正在显示的窗口)。
我找到了一个教程,展示了如何使用 gdi32 API 来执行此操作,但由于这是一个 WPF 应用程序,我正在寻找一种不使用 GDI 来执行此操作的方法。
我还找到了如何使用 CroppedBitmap 从图片中检索像素的示例;但是,我没有在我的应用程序中使用图像。
有谁知道如何实现这一目标?
谢谢!
I am working on a WPF application and I am looking to retrieve the color value of a pixel on the screen (the screen being the Window that my application is displaying).
I have found a tutorial that shows how to do this using the gdi32 API but since this is a WPF application I am looking for a way to do this without using GDI.
I have also found an example of how to retrieve a pixel from within a picture using a CroppedBitmap; however, I'm not using an image in my application.
Does anyone know how to achieve this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
完全披露:我为 WriteableBitmapEx 开源项目做出了贡献,但它不是我的库,也不隶属于其所有者
我知道在普通 WPF 中执行此操作的唯一方法是使用 RenderTargetBitmap将画布渲染为位图,然后使用 WriteableBitmap API 获取指向相关像素的指针。这将是一个非常慢的操作,因为渲染操作涉及创建临时(软件)表面,但是它适合不频繁的操作,例如绘制“颜色选择器”类型的应用程序。
另请参阅 WriteableBitmapEx 库,因为它具有 GetPixel 和 SetPixel 函数,这将在本示例中为您提供帮助。
Full Disclosure: I have contributed to the WriteableBitmapEx open source project, however it is not my library nor am I affiliated with its owner
The only way that I know to do this in vanilla WPF is to use RenderTargetBitmap to render the canvas to a bitmap, then use the WriteableBitmap API to get a pointer to the pixel in question. This will be a very slow operation as the render operation involves creation of a temporary (software) surface, however it would be suitable for infrequent operations, such as paint "color picker" type applications.
Also see the WriteableBitmapEx library, as this has functions for GetPixel and SetPixel which will help you in this example.