private void ButtonSave_Click(object sender, RoutedEventArgs e) { if (checkValidity(out float size) && savePicture(size)) { Close(); } bool checkValidity(out float size) { if (!float.TryParse(_textBoxSize.Text, out size)) { MessageBox.Show("Please check your input. The input is invalid.", "Info"); size = default; return(!(e.Handled = true)); } return(true); } bool savePicture(float size) { var saveFileDialog = new SaveFileDialog { AddExtension = true, DefaultExt = "png", Filter = "PNG files|*.png|JPG files|*.jpg|BMP files|*.bmp|GIF files|*.gif|WMF files|*.wmf", Title = "Save picture..." }; if (!(saveFileDialog.ShowDialog() is true)) { return(!(e.Handled = true)); } var pc = new PointConverter(size, size); var layerCollection = new LayerCollection { new BackLayer(pc, _settings.BackgroundColor), new GridLineLayer( pc, _settings.GridLineWidth, _settings.GridLineColor), new BlockLineLayer( pc, _settings.BlockLineWidth, _settings.BlockLineColor), new ValueLayer( pc, _settings.ValueScale, _settings.CandidateScale, _settings.GivenColor, _settings.ModifiableColor, _settings.CandidateColor, _settings.GivenFontName, _settings.ModifiableFontName, _settings.CandidateFontName, _grid, _settings.ShowCandidates), }; if (_oldCollection[typeof(CustomViewLayer)] is CustomViewLayer customViewLayer) { layerCollection.Add(new CustomViewLayer(pc, customViewLayer)); } if (_oldCollection[typeof(ViewLayer)] is ViewLayer viewLayer) { layerCollection.Add(new ViewLayer(pc, viewLayer)); } Bitmap?bitmap = null; try { bitmap = new Bitmap((int)size, (int)size); int selectedIndex = saveFileDialog.FilterIndex; string fileName = saveFileDialog.FileName; if (selectedIndex >= -1 && selectedIndex <= 3) { // Normal picture formats. layerCollection.IntegrateTo(bitmap); var encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L); bitmap.Save( fileName, GetEncoderInfo( selectedIndex switch { -1 => Png, 0 => Png, 1 => Jpeg, 2 => Bmp, 3 => Gif, _ => throw Throwings.ImpossibleCase }) ?? throw new NullReferenceException("The return value is null."), encoderParameters); }