public MainWindow() { InitializeComponent(); //Connect to the server and subscribe to the events we want imager = new ImagerRemote(serverName); imager.DescriptionReceived += new DescriptionReceivedEventHandler(imager_DescriptionReceived); imager.FramesDiscarded += new FramesDiscardedEventHandler(imager_FramesDiscarded); imager.FrameReceived += new FrameReceivedEventHandler(imager_FrameReceived); imager.MuteWarnings = true; imager.UnbindChannels(); //This isn't required here, but it doesn't hurt anything //Put the name of the server on the GUI ServerNameLabel.Content = "Listening to server: " + serverName; //Use a timer to keep the VRPN connection updated timer = new DispatcherTimer(); timer.Tick += new EventHandler(timer_Tick); timer.Interval = new TimeSpan(0, 0, 0, 0, 15); timer.Start(); thisProc = System.Diagnostics.Process.GetCurrentProcess(); thisProc.ErrorDataReceived += thisProc_ErrorDataReceived; thisProc.OutputDataReceived += thisProc_ErrorDataReceived; }
//Update the display parameters based on the image description void imager_DescriptionReceived(object sender, DescriptionReceivedEventArgs e) { imager.UnbindChannels(); //Unbind the channels so we can set the new channel bindings base on the new description //Save the size of the image to class variables so we can use it later width = e.Columns; height = e.Rows; depth = e.Depth; //Setup the colors if (imager.Channels.Length == 1) { //Grayscale doesn't require channel binding if (imager.Channels[0].BitDepth == ImageBitDepth.unsigned8bit) { imageFormat = PixelFormats.Gray8; bytesPerPixel = 1; bytesPerChannel = 1; } else if (imager.Channels[0].BitDepth == ImageBitDepth.float32bit) { imageFormat = PixelFormats.Gray32Float; bytesPerPixel = 4; bytesPerChannel = 4; } else { imageFormat = PixelFormats.Gray16; bytesPerPixel = 2; bytesPerChannel = 2; } } else if (imager.Channels.Length == 3) { //For RGB, we will bind the first 3 channels //In a real application, it would be preferable to bind by channel name if (imager.Channels[0].BitDepth == ImageBitDepth.unsigned8bit) { imageFormat = PixelFormats.Rgb24; bytesPerPixel = 3; bytesPerChannel = 1; if (useChannelBinding) { imager.BindChannels("BoundVirtualChannel", imager.Channels[0].Name, 0, imager.Channels[1].Name, 1, imager.Channels[2].Name, 2); } } else if (imager.Channels[0].BitDepth == ImageBitDepth.float32bit) { //WPF doesn't support a 96 bits-per-pixel RGB format throw new Exception("Format not supported."); } else { imageFormat = PixelFormats.Rgb48; bytesPerPixel = 6; bytesPerChannel = 2; if (useChannelBinding) { imager.BindChannels("BoundVirtualChannel", imager.Channels[0].Name, 0, imager.Channels[1].Name, 2, imager.Channels[2].Name, 4); } } } else if (imager.Channels.Length == 4) { //Similiarly, for RGBA we will bind the first 4 channels //In a real application, it would be preferable to bind by channel name if (imager.Channels[0].BitDepth == ImageBitDepth.unsigned8bit) { //Since this format is bgra instead of rgba, we reverse the order of the channels assuming blue will be the 3rd channel and red the 1st imageFormat = PixelFormats.Bgra32; bytesPerPixel = 4; bytesPerChannel = 1; if (useChannelBinding) { imager.BindChannels("BoundVirtualChannel", imager.Channels[2].Name, 0, imager.Channels[1].Name, 1, imager.Channels[0].Name, 2, imager.Channels[3].Name, 3); } } else if (imager.Channels[0].BitDepth == ImageBitDepth.float32bit) { imageFormat = PixelFormats.Rgba128Float; bytesPerPixel = 16; bytesPerChannel = 4; if (useChannelBinding) { imager.BindChannels("BoundVirtualChannel", imager.Channels[0].Name, 0, imager.Channels[1].Name, 4, imager.Channels[2].Name, 8, imager.Channels[3].Name, 12); } } else { imageFormat = PixelFormats.Rgba64; bytesPerPixel = 8; bytesPerChannel = 2; if (useChannelBinding) { imager.BindChannels("BoundVirtualChannel", imager.Channels[0].Name, 0, imager.Channels[1].Name, 2, imager.Channels[2].Name, 4, imager.Channels[3].Name, 6); } } } if (bytesPerPixel != 0) { image = new WriteableBitmap(width, height, 96.0, 96.0, imageFormat, null); DisplayedImage.Source = image; rawImage = new byte[e.Rows * e.Columns * e.Depth * bytesPerPixel]; ConsoleTextBox.Text += String.Format("New description Received at: {0}, the new size is {1} x {2} x {3} with a format of " + imageFormat.ToString() + "\r\n", e.Time, e.Rows, e.Columns, e.Depth); } }