public void ImageFromFileToDatabase() { // Configure open file dialog box Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.FileName = ""; // Default file name dlg.DefaultExt = ".wgi"; // Default file extension dlg.Filter = "WaveGuide Image (.wgi)|*.wgi"; // Filter files by extension // Show open file dialog box Nullable <bool> result = dlg.ShowDialog(); // Process open file dialog box results if (result == true) { // Open document string filename = dlg.FileName; if (File.Exists(filename)) { byte[] image; ReadImageFromWgiFile(out image, filename); int numbytes = image.Length; ReferenceImageContainer cont = new ReferenceImageContainer(); cont.Width = 1024; cont.Height = 1024; cont.Depth = 2; Buffer.BlockCopy(image, 0, cont.ImageData, 0, numbytes); //cont.ImageData = image; cont.TimeStamp = DateTime.Now; cont.NumBytes = numbytes; bool success = wgDB.InsertReferenceImage(ref cont); if (success) { success = true; } } } }
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(); }
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; bool success = wgDB.InsertReferenceImage(ref refCont); if (!success) { System.Windows.MessageBox.Show("Failed to insert Reference Image: " + wgDB.GetLastErrorMsg(), "Database Error", MessageBoxButton.OK, MessageBoxImage.Error); } } Close(); }