void ColorClient_FrameReady(object sender, FrameReadyEventArgs e) { MemoryStream ms = new MemoryStream(e.Data); BinaryReader br = new BinaryReader(ms); ColorFrameReadyEventArgs args = new ColorFrameReadyEventArgs(); ColorFrameData cfd = new ColorFrameData { Format = (ImageFormat)br.ReadInt32(), ImageFrame = br.ReadColorImageFrame() }; if(cfd.Format == ImageFormat.Raw) cfd.RawImage = br.ReadBytes(e.Data.Length - sizeof(bool)); else { BitmapImage bi = new BitmapImage(); bi.SetSource(new MemoryStream(e.Data, (int)ms.Position, (int)(ms.Length - ms.Position))); cfd.BitmapImage = bi; } ColorFrame = cfd; args.ColorFrame = cfd; if(ColorFrameReady != null) ColorFrameReady(this, args); }
private async void ColorThread() { try { var reader = new DataReader( Client.InputStream ); reader.InputStreamOptions = InputStreamOptions.Partial; reader.ByteOrder = ByteOrder.LittleEndian; while ( IsConnected ) { await reader.LoadAsync( 4 ); var size = reader.ReadUInt32(); await reader.LoadAsync( size ); byte[] bytes = new byte[size]; reader.ReadBytes( bytes ); MemoryStream ms = new MemoryStream( bytes ); BinaryReader br = new BinaryReader(ms); ColorFrameReadyEventArgs args = new ColorFrameReadyEventArgs(); ColorFrameData cfd = new ColorFrameData(); cfd.Format = (ImageFormat)br.ReadInt32(); cfd.ImageFrame = br.ReadColorImageFrame(); MemoryStream msData = new MemoryStream( bytes, (int)ms.Position, (int)(ms.Length - ms.Position) ); if (cfd.Format == ImageFormat.Raw) { cfd.RawImage = ms.ToArray(); } else { InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream(); DataWriter dw = new DataWriter(ras.GetOutputStreamAt(0)); dw.WriteBytes(msData.ToArray()); await dw.StoreAsync(); // Set to the image BitmapImage bImg = new BitmapImage(); bImg.SetSource(ras); cfd.BitmapImage = bImg; } ColorFrame = cfd; args.ColorFrame = cfd; if (ColorFrameReady != null) { ColorFrameReady(this, args); } } } catch(IOException) { Disconnect(); } }
private void ColorThread() { try { NetworkStream ns = Client.GetStream(); BinaryReader reader = new BinaryReader(ns); while(Client.Connected) { ColorFrameReadyEventArgs args = new ColorFrameReadyEventArgs(); ColorFrameData cfd = new ColorFrameData(); int size = reader.ReadInt32(); byte[] data = reader.ReadBytes(size); MemoryStream ms = new MemoryStream(data); BinaryReader br = new BinaryReader(ms); cfd.Format = (ImageFormat)br.ReadInt32(); cfd.ImageFrame = br.ReadColorImageFrame(); MemoryStream msData = new MemoryStream(data, (int)ms.Position, (int)(ms.Length - ms.Position)); Context.Send(delegate { if(cfd.Format == ImageFormat.Raw) cfd.RawImage = ms.ToArray(); else { BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = msData; bi.EndInit(); cfd.BitmapImage = bi; } ColorFrame = cfd; args.ColorFrame = cfd; if(ColorFrameReady != null) ColorFrameReady(this, args); }, null); } } catch(IOException) { Client.Close(); } }