“实时”更新 WPF 控件
我正在编写一个应用程序,它将显示相机看到的当前图像,并且需要实时或接近更新显示的图像。本质上,我有一台可以捕捉图像的相机,我需要每隔(比如 1 秒)捕捉一个图像并将该图像显示到屏幕上。目前,我的应用程序有一个 Image 控件,我正在从相机捕获 BitmapImage 并将其设置为 Image.Source。我的麻烦是让它不断更新。不幸的是,我没有处理类似这样的事情的经验,这些事情必须永远更新自己(或者直到我正在编写的应用程序关闭),而且老实说,似乎几乎没有(我已经能够发掘) web 有关在 WPF/C# 中执行类似操作的信息。我怀疑我必须生成一个线程来执行图像捕获,但老实说,这是我问题的一部分——我对使用线程的经验很少,并且对所有这些是如何工作的感到有点困惑。非常感谢您提供的任何帮助。
I'm writing an application which will display the current image seen by a camera and it needs to update the shown image in real time, or close to it. Essentially, I have a camera with which I can capture images and I need to capture one every, say, 1 second and display that image to the screen. Currently, my application has an Image control and I'm capturing a BitmapImage from the camera and setting this as the Image.Source. My trouble is getting this to continuously update. Unfortunately, I have no experience dealing with something like this that has to update itself forever (or until the application I'm writing is closed) and honestly there seems to be very little to none (that I have been able to unearth) on the web about doing something like this in WPF/C#. I suspect I'll have to spawn a thread to perform the image capturing, but honestly, that's part of my issue--I have very little experience working with threads and am a bit confused on how all that works. Thanks so much for any help you can provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要使数据绑定正确更新,您可以使用 INotifyPropertyChanged。只需添加对 System.ComponentModel 的引用:
然后继承接口:
然后添加以下代码:
最后,添加要绑定到的属性。
最后,在您的 xaml 中,将绑定更改为 {Binding CurrentImage},然后对于窗口,将数据上下文设置为相对源自身...这将是窗口的属性:
这应该使绑定正常工作。在单独的线程上执行操作需要调度程序
To make the data binding get updated properly, you can use INotifyPropertyChanged. Just add a reference to System.ComponentModel:
Then inherit the interface:
Then add the following code:
Finally, add your property that you want to bind to.
Finally, in your xaml, change the binding to {Binding CurrentImage} and then for the window, set the data context to relative source self... this would be a property for the window:
That should get the binding working properly. Doing things on a separate thread would require the dispatcher
您应该阅读 WPF 中的数据绑定。 WPF 充满了观察者模式,一旦绑定的数据项发生更改,就会更新显示。事实上,您正在更改 Image.Source 而不是更改图像的内容,这可能是令人困惑的事情。您需要在 XAML 中将图像控件设置为数据绑定到位图对象(可能是表单的字段),然后根据需要更改或重新加载位图对象。每次对位图对象进行更改时,数据绑定图像控件都应自动重绘自身。
由于您提到了后台线程,因此您需要小心地仅在 UI 线程上修改 UI 元素(图像、位图)的属性。如果您在后台线程中收到新图像并希望在 UI 中显示它,则需要将该更新同步到 UI 线程。请参阅 调度程序.Invoke()
You should read up on data binding in WPF. WPF is riddled with observer patterns to update the display as soon as a bound data item is changed. The fact that you are changing the Image.Source instead of changing the content of the image may be what's confusing things. You need to set up the image control in XAML as data-bound to a bitmap object (probably a field of your form) and then alter or reload the bitmap object as needed. The data-bound image control should redraw itself automatically with each change to the bitmap object.
Since you mention background threads, you need to be careful to modify the properties of UI elements (Image, Bitmap) only on the UI thread. If you receive a new image in the background thread and want to show it in the UI, you will need to synchronize that update to the UI thread. See Dispatcher.Invoke()
我处于类似的情况,只是图像是通过 WCF 传递到我的 WPF 客户端。所以我设置了一个计时器并每隔 1 秒调用一次 WCF 服务。但是当我将 ImageSource 分配给 bitmapImage 时,图像会闪烁,就像我得到一个白屏,图像,白色屏幕,图像
如何使其连续?
I am in a similar situation, except that the Image is passes to my WPF client via WCF. So I have set a timer and call the WCf service every1 second. But when I assign the ImageSource to the bitmapImage the image is flickering, as in I get a white screen ,Image, whit screen, image
How to make it continuous?