/// <summary> /// 播放线程执行方法 /// </summary> private unsafe void DeCoding() { try { Console.WriteLine("DeCoding run..."); //Bitmap oldBmp = null; // 更新图片显示 tstRtmp.ShowBitmap show = (width, height, stride, data) => { // // this.Invoke(new MethodInvoker(() => // // { // // this.pic.Image = bmp; // // if (oldBmp != null) // // { // // oldBmp.Dispose(); // // } // // oldBmp = bmp; // // })); WriteableBitmap Wb = null; Int32Rect rec = Int32Rect.Empty; this.Dispatcher.Invoke(new Action(delegate { //this.LiveImg.Source = bmp; //Solution 1: Official Way //IntPtr ptr = bmp.GetHbitmap(); //BitmapSource Bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( // ptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); ////release resource //DeleteObject(ptr); //this.LiveImg.Source = Bs; //Solution 2: Memory Way //BitmapImage Bi = BitmapToBitmapImage(bmp); //LiveLU.Source = Bi; //Solution 3: WriteableBitmap //Low Frame Rate if (Wb == null) { Wb = new WriteableBitmap(width, height, 96, 96, System.Windows.Media.PixelFormats.Bgr24, null); rec = new Int32Rect(0, 0, width, height); } Wb.Lock(); Wb.AddDirtyRect(rec); Wb.WritePixels(rec, data, width * height * 4, stride); //Debug.WriteLine(frameNumber); Wb.Unlock(); this.LiveImg.Source = Wb; })); }; rtmp.Start(show, "rtmp://127.0.0.1/live"); } catch (Exception ex) { Console.WriteLine(ex); } finally { Console.WriteLine("DeCoding exit"); rtmp.Stop(); thPlayer = null; //this.Invoke(new MethodInvoker(() => //{ // btnStart.Text = "开始播放"; // btnStart.Enabled = true; //})); } }