【问题标题】:C# MemoryStream slowing programme performanceC# MemoryStream 降低程序性能
【发布时间】:2013-06-13 09:12:25
【问题描述】:

我正在使用 WPF 来显示 Kinect ColorImageFrame 和骨架表示的项目。我还要录那两个视频。

我能够显示和记录(使用 EmguCV)这两个图像,但是我遇到了一些性能问题。看来我这部分代码是我性能下降的原因。

private void DrawSkeleton(Skeleton[] skeletons)
    {
        using (System.Drawing.Bitmap skelBitmap = new System.Drawing.Bitmap(640, 480))
        {
            foreach (Skeleton S in skeletons)
            {
                if (S.TrackingState == SkeletonTrackingState.Tracked)
                {
                    DrawBonesAndJoints(S,skelBitmap);                        
                }
                else if (S.TrackingState == SkeletonTrackingState.PositionOnly)
                {

                }
            }
            _videoArraySkel.Add(ToOpenCVImage<Bgr, Byte>(skelBitmap));
            BitmapSource source = ToWpfBitmap(skelBitmap);
            this.skeletonStream.Source = source;       
        }            
    }

更准确地说,来自 ToWpfBitmap,它允许我在我的窗口中显示它:

public static BitmapSource ToWpfBitmap(System.Drawing.Bitmap bitmap) 
    {
        using (MemoryStream stream = new MemoryStream()) 
        {
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            stream.Position = 0;
            BitmapImage result = new BitmapImage();
            result.BeginInit();
            // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
            // Force the bitmap to load right now so we can dispose the stream.
            result.CacheOption = BitmapCacheOption.OnLoad;
            result.StreamSource = stream;
            result.EndInit();
            result.Freeze();
            return result;
        }
    }

性能损失的特点是: - Window 上显示的视频不再流畅 - 视频录制似乎错过了一些帧,这导致视频比正常速度更快/更低。

你能帮我告诉我这个问题可能来自哪里吗?

【问题讨论】:

  • 也许这篇关于Bitmap to BitmapSource转换的CodeProject文章可以帮助你。您是否尝试过使用 System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap 方法?
  • 我已经尝试过使用这种方法,但奇怪的是位图不再显示在我的窗口中......

标签: c# wpf bitmap emgucv memorystream


【解决方案1】:

尝试使用RecyclableMemoryStream 代替 MemoryStream。它旨在解决一些内存问题。

查看这篇文章了解详情 - Announcing Microsoft.IO.RecycableMemoryStream

【讨论】:

    【解决方案2】:

    您是否尝试过在单独的线程中执行内存写入 i/o,同时将数据保留在像队列一样的缓冲区中?

    【讨论】:

      猜你喜欢
      • 2020-06-02
      • 2012-06-22
      • 2023-04-05
      • 1970-01-01
      • 2021-11-19
      • 2013-11-16
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      相关资源
      最近更新 更多