【问题标题】:Updating a WPF image source from a threaded callback in a c++ dll从 c++ dll 中的线程回调更新 WPF 图像源
【发布时间】:2019-09-18 15:31:49
【问题描述】:

我有一个连接到网络摄像头的 c++ dll。我正在尝试将这些帧调用到 c# wpf 应用程序中进行显示。

我有一个在 dll 中运行并通过回调将数据发回的线程,如下所示:Updated 函数在每个新帧被调用,并更新 zedframe 数据;

// class A
//callback from a c++ dll.
        public IntPtr zedFrame;
        public Mutex mutex = new Mutex();
        public bool bGotFrame = false;

 public event EventHandler Updated;

        protected virtual void Updated(EventArgs e, IntPtr frame)
        {

            mutex.WaitOne();   // Wait until it is safe to enter.  
            zedFrame = frame;
            mutex.ReleaseMutex();    // Release the Mutex.

            bGotFrame = true;

        }

在同一个类中,我有这个函数“应该”以线程安全的方式获取帧数据。

public IntPtr getFrame()
        {
            IntPtr tmp;
            mutex.WaitOne();   // Wait until it is safe to enter.  
            tmp = zedFrame;
            mutex.ReleaseMutex();    // Release the Mutex.

            return tmp;
        }

在 wpf 表单中,我有一张图片:

 <Grid>
        <Image Name="ImageCameraFrame" Margin="5,5,5,5" MouseDown="GetImageCoordsAt"/>       
    </Grid>

在表单的cs中,我有一个DispatcherTimer

//以图像形式.xaml.cs

 var timer = new DispatcherTimer
                {
                    Interval = TimeSpan.FromMilliseconds(100)
                };

                timer.Tick += Timer_Tick;

                 private void Timer_Tick(object sender, EventArgs e)
        {
            if (ClassA.bGotFrame == true)
            {
                var data = ClassA.getFrame();
                var width = 1280;
                var height = 720;
                var stride = 3 * width;
                var bitmap = BitmapSource.Create(width, height, 96, 96,
                    PixelFormats.Rgb24, null, data, stride * height, stride);
                bitmap.Freeze();


                ImageCameraFrame.Source = bitmap;

            }

        }

当我运行它时,它会连接,显示一个帧,然后使应用程序崩溃。 我正在冻结位图,并使用互斥锁。 我做错了什么?

是否可以将图像源绑定到另一个类中的变量,在另一个线程中更新?还是我需要计时器?

【问题讨论】:

  • 您可以使用简单的lock(){},而不是处理互斥锁。冻结意味着它现在是线程安全的,回调在哪个线程上,是 UI 还是后台?
  • 后台回调。
  • 可能是计时器在每个图像帧中不止一次滴答作响,并且在同一帧中将ClassA.bGotFrame 视为true - 然后尝试使用相同的内存图像制作两个或更多图像?也许尝试设置bGotFrame = false;in getFrame()
  • 我还建议不要为每一帧创建一个新的 BitmapSource。相反,将单个 WriteableBitmap 分配给 Image 的 Source 属性,然后重复更新其 BackBuffer 属性:stackoverflow.com/a/12587470/1136211

标签: c# wpf


【解决方案1】:

我的情况有点类似,最终采用了以下方法。也许使用 GDI 清理进行 ImageSource 转换会对您有所帮助。

Xaml:

<Image DockPanel.Dock="Left" Source="{Binding AssistantLogo}"
       RenderOptions.BitmapScalingMode="NearestNeighbor" UseLayoutRounding="True"
       Width="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelWidth}" 
       Height="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelHeight}"/>

视图模型:

if (Patient.ContainsMeaningfulData(applicationState.Patient))
  _viewModel.AssistantLogo = bitmap1.ToImageSource();
else
  _viewModel.AssistantLogo = bitmap2.ToImageSource();

扩展方法:

using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;

    /// <summary>
    /// Convert a winforms image to a wpf-usable imageSource
    /// </summary>
    /// <param name="bitmap">winforms image</param>
    /// <returns>wpf imageSource object</returns>
    public static ImageSource ToImageSource(this System.Drawing.Bitmap bitmap)
    {
      var hbitmap = bitmap.GetHbitmap();
      try
      {
        var imageSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bitmap.Width, bitmap.Height));

        return imageSource;
      }
      finally
      {
        Gdi32.DeleteObject(hbitmap);
      }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-29
    • 2016-01-29
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    相关资源
    最近更新 更多