public static Color ToGray(this Color color) { return(UtilityWPF.ConvertToGray(color)); }
private void TakePicture_Click(object sender, RoutedEventArgs e) { try { int size; if (!int.TryParse(txtImageSize.Text, out size)) { MessageBox.Show("Couldn't parse image size as an integer", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning); return; } else if (size < 1) { MessageBox.Show("Size must be at least 1", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (radColor.IsChecked.Value) { // No need to convert in/out of bitmap source image.Source = UtilityWPF.RenderControl(grdViewPort, size, size, true); return; } // Render the control BitmapCustomCachedBytes bitmap = null; if (radGrayTransparent.IsChecked.Value) { bitmap = (BitmapCustomCachedBytes)UtilityWPF.RenderControl(grdViewPort, size, size, false, Colors.Transparent, true); } else if (radGrayBlack.IsChecked.Value) { Brush background = grdViewPort.Background; grdViewPort.Background = Brushes.Black; grdViewPort.UpdateLayout(); bitmap = (BitmapCustomCachedBytes)UtilityWPF.RenderControl(grdViewPort, size, size, false, Colors.Black, true); grdViewPort.Background = background; } else { throw new ApplicationException("Unknown radio button"); } // Convert to gray var colors = bitmap.GetColors_Byte(). Select(o => { byte gray = Convert.ToByte(UtilityWPF.ConvertToGray(o[1], o[2], o[3])); return(new byte[] { o[0], gray, gray, gray }); }). ToArray(); // Show it image.Source = UtilityWPF.GetBitmap(colors, size, size); } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }