WPF - 更新“System.Windows.Controls.Image”来自另一个线程

发布于 2024-10-11 19:10:38 字数 808 浏览 2 评论 0原文

我在该代码上遇到了这个异常。 如何修复它?

例外:

调用线程无法访问此 对象,因为不同的线程拥有 它。

代码:

    void CamProc_NewTargetPosition(IntPoint Center, System.Drawing.Bitmap image)
    {
        IntPtr hBitMap = image.GetHbitmap();
        BitmapSource bmaps = Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

        Dispatcher.BeginInvoke((Action)(() =>
        {
            labelX.Content = String.Format("X: {0}", Center.X); //OK Working
            labelY.Content = String.Format("Y: {0}", Center.Y); //OK Working
            pictureBoxMain.Source = bmaps; // THERE IS EXCEPTON
        }), DispatcherPriority.Render, null);

    }

pictureBoxMain是System.Windows.Controls.Image。

I get this exception on that code.
How to fix it?

Excepton:

The calling thread cannot access this
object because a different thread owns
it.

Code:

    void CamProc_NewTargetPosition(IntPoint Center, System.Drawing.Bitmap image)
    {
        IntPtr hBitMap = image.GetHbitmap();
        BitmapSource bmaps = Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

        Dispatcher.BeginInvoke((Action)(() =>
        {
            labelX.Content = String.Format("X: {0}", Center.X); //OK Working
            labelY.Content = String.Format("Y: {0}", Center.Y); //OK Working
            pictureBoxMain.Source = bmaps; // THERE IS EXCEPTON
        }), DispatcherPriority.Render, null);

    }

pictureBoxMain is System.Windows.Controls.Image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亣腦蒛氧 2024-10-18 19:10:38

您可以冻结 BitmapSource,以便可以从任何线程访问它:

void CamProc_NewTargetPosition(IntPoint Center, System.Drawing.Bitmap image)
    {
        IntPtr hBitMap = image.GetHbitmap();
        BitmapSource bmaps = Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        bmaps.Freeze();

        Dispatcher.BeginInvoke((Action)(() =>
        {
            labelX.Content = String.Format("X: {0}", Center.X);
            labelY.Content = String.Format("Y: {0}", Center.Y);
            pictureBoxMain.Source = bmaps;
        }), DispatcherPriority.Render, null);

    }

You can freeze the BitmapSource so that it can be accessed from any thread:

void CamProc_NewTargetPosition(IntPoint Center, System.Drawing.Bitmap image)
    {
        IntPtr hBitMap = image.GetHbitmap();
        BitmapSource bmaps = Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
        bmaps.Freeze();

        Dispatcher.BeginInvoke((Action)(() =>
        {
            labelX.Content = String.Format("X: {0}", Center.X);
            labelY.Content = String.Format("Y: {0}", Center.Y);
            pictureBoxMain.Source = bmaps;
        }), DispatcherPriority.Render, null);

    }
罪#恶を代价 2024-10-18 19:10:38

您可以按照另一个线程中的建议冻结图像,这可以摆脱线程限制,但使图像不可变。

WPF/BackgroundWorker 和 BitmapSource 问题

You could Freeze the image, as suggested in another thread, which gets rid of the threading restriction but makes the image immutable.

WPF/BackgroundWorker and BitmapSource problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文