Esempio n. 1
0
        /// <summary>
        /// Converts the original image into a grayscale
        /// </summary>
        private Convolution2D GetOriginalImageGrays()
        {
            if (originalImage.Source == null)
            {
                return(null);
            }

            int?limit = null;
            int limitInt;

            if (chkLimitImageSize.IsChecked.Value && int.TryParse(txtSizeLimit.Text, out limitInt))
            {
                limit = limitInt;
            }

            bool shouldBuild = false;

            if (_origImageGrays == null)
            {
                shouldBuild = true;
            }
            else if (limit != null && (_origImageGrays.Width > limit.Value || _origImageGrays.Height > limit.Value))
            {
                // The current one is the wrong size
                shouldBuild = true;
            }

            if (shouldBuild)
            {
                BitmapSource bitmap = (BitmapSource)originalImage.Source;

                BitmapCustomCachedBytes colors = null;

                if (limit != null)
                {
                    bitmap = UtilityWPF.ResizeImage(bitmap, limit.Value);       // this will only resize if it's too big
                }

                colors = (BitmapCustomCachedBytes)UtilityWPF.ConvertToColorArray(bitmap, false, Colors.Transparent);

                _origImageGrays = colors.ToConvolution();
            }

            return(_origImageGrays);
        }
Esempio n. 2
0
        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);
            }
        }