private void SavePB_Click(object sender, RoutedEventArgs e) { if (VM.SaveAsFile) { if (VM.Filename.Length == 0) { System.Windows.MessageBox.Show("Filename cannot be empty.", "Enter a Filename", MessageBoxButton.OK, MessageBoxImage.Warning); return; } string path = VM.Location + "\\" + VM.Filename + ".png"; if (File.Exists(path)) { MessageBoxResult result = System.Windows.MessageBox.Show("File already exists. Do you want to OverWrite it?", "File Already Exists", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.No) { return; } } using (FileStream stream = new FileStream(path, FileMode.Create)) { PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(m_bitmap.Clone())); encoder.Save(stream); stream.Close(); } } else { if (VM.Description.Length == 0) { System.Windows.MessageBox.Show("Description cannot be empty for the Reference Image.", "Enter a Description", MessageBoxButton.OK, MessageBoxImage.Warning); return; } // save as reference image in database WaveguideDB wgDB = new WaveguideDB(); ReferenceImageContainer refCont = new ReferenceImageContainer(); refCont.CompressionAlgorithm = GlobalVars.CompressionAlgorithm; refCont.Depth = 2; refCont.Height = m_height; refCont.Width = m_width; refCont.ImageData = m_imageData; refCont.MaxPixelValue = GlobalVars.MaxPixelValue; refCont.NumBytes = m_imageData.Length * 2; refCont.TimeStamp = DateTime.Now; refCont.Description = VM.Description; refCont.Type = VM.Type; bool alreadyExists; bool success = wgDB.ReferenceImageTypeAlreadyExists(VM.Type, out alreadyExists); if (success) { if (!alreadyExists) { success = wgDB.InsertReferenceImage(ref refCont); if (!success) { System.Windows.MessageBox.Show("Failed to insert Reference Image: " + wgDB.GetLastErrorMsg(), "Database Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } } else { string sMessageBoxText = "This reference already exists for: " + VM.Type.ToString() + ". Do you want to replace it?"; string sCaption = "Reference Image Already Exists For This Type"; MessageBoxButton btnMessageBox = MessageBoxButton.YesNo; MessageBoxImage icnMessageBox = MessageBoxImage.Warning; MessageBoxResult rsltMessageBox = System.Windows.MessageBox.Show(sMessageBoxText, sCaption, btnMessageBox, icnMessageBox); switch (rsltMessageBox) { case MessageBoxResult.Yes: // delete the existing reference image success = wgDB.ClearReferenceImageType(VM.Type); if (success) { // insert the new reference image success = wgDB.InsertReferenceImage(ref refCont); if (!success) { System.Windows.MessageBox.Show("Failed to insert Reference Image: " + wgDB.GetLastErrorMsg(), "Database Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else { System.Windows.MessageBox.Show("Failed to clear Reference Image: " + wgDB.GetLastErrorMsg(), "Database Error", MessageBoxButton.OK, MessageBoxImage.Error); } break; case MessageBoxResult.No: // do nothing return; } } } } Close(); }