private void Button_Click_2(object sender, RoutedEventArgs e) { BitmapFrame croppedBitmapFrame = CroppingAdorner.GetCroppedBitmapFrame(); //create PNG image BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(croppedBitmapFrame)); //save image to file SaveFileDialog dlg = new SaveFileDialog(); dlg.FileName = "TestCropping"; // Default file name dlg.DefaultExt = ".png"; // Default file extension dlg.Filter = "Image png (.png)|*.png"; // Filter files by extension // Show save file dialog box Nullable <bool> result = dlg.ShowDialog(); // Process save file dialog box results if (result == true) { // Save document string filename = dlg.FileName; using (FileStream imageFile = new FileStream(filename, FileMode.Create, FileAccess.Write)) { encoder.Save(imageFile); imageFile.Flush(); imageFile.Close(); } } }
/// <summary> /// Initialize Cropping Adorner and find puzzle pieces image /// </summary> private void PieceBackgroundColorPickerWindow_Loaded(object sender, RoutedEventArgs e) { AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(CroppingCanvasPanel); CroppingAdorner = new CroppingAdorner(CroppingCanvasPanel); adornerLayer.Add(CroppingAdorner); CroppingAdorner.OnRectangleSizeChangedEvent += (s, args) => { CroppedImage = CroppingAdorner.GetCroppedBitmapFrame(); }; CroppingAdorner.OnRectangleLocationChangedEvent += (s, args) => { CroppedImage = CroppingAdorner.GetCroppedBitmapFrame(); }; CroppingAdorner.SizeChanged += (s, args) => { CroppedImage = CroppingAdorner.GetCroppedBitmapFrame(); }; if (Directory.Exists(_puzzlePiecesFolderPath)) { List <string> imageExtensions = new List <string>() { ".jpg", ".png", ".bmp", ".tiff" }; DirectoryInfo dirInfo = new DirectoryInfo(_puzzlePiecesFolderPath); List <FileInfo> fileInfos = dirInfo.GetFiles("*.*").ToList(); fileInfos = fileInfos.Where(f => imageExtensions.Contains(f.Extension)).ToList(); PuzzlePieceImageFileName = fileInfos?.First().FullName; } }