Ejemplo n.º 1
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();

            save.InitialDirectory = strInitalDirectory;

            if (1 < GetSelectedItemCount())
            {
                save.Filter = "TIF|*.tif|PDF|*.pdf";
            }
            else
            {
                save.Filter = "TIF|*.tif|PDF|*.pdf|JPG|*.jpg";
            }

            bool?result = save.ShowDialog();

            fileSaveStatus = ScanFileSaveError.FileSave_OK;

            if (result == true)
            {
                strInitalDirectory = save.FileName;

                int position = strInitalDirectory.LastIndexOf('\\');
                if (position > 0)
                {
                    strInitalDirectory = strInitalDirectory.Substring(0, position);
                }

                if (!IsTempImageExist())
                {
                    VOP.Controls.MessageBoxEx.Show(
                        VOP.Controls.MessageBoxExStyle.Simple,
                        m_MainWin,
                        (string)this.FindResource("ResStr_Image_file_not_found"),
                        (string)this.FindResource("ResStr_Error")
                        );

                    return;
                }

                if (false == DoseHasEnoughSpace(save.FileName))
                {
                    VOP.Controls.MessageBoxEx.Show(
                        VOP.Controls.MessageBoxExStyle.Simple,
                        m_MainWin,
                        (string)this.FindResource("ResStr_Operation_cannot_be_carried_out_due_to_insufficient_memory_or_hard_disk_space_Please_try_again_after_freeing_memory_or_hard_disk_space_"),
                        (string)this.FindResource("ResStr_Error")
                        );

                    return;
                }

                // This index is 1-based, not 0-based
                List <string> files = new List <string>();
                GetSelectedFile(files);

                Thread thread = new Thread(() =>
                {
                    try
                    {
                        if (3 == save.FilterIndex)
                        {
                            JpegBitmapEncoder encoder = new JpegBitmapEncoder();

                            foreach (string path in files)
                            {
                                Uri myUri = new Uri(path, UriKind.RelativeOrAbsolute);
                                BmpBitmapDecoder decoder = new BmpBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.None);
                                BitmapSource origSource  = decoder.Frames[0];

                                if (null != origSource)
                                {
                                    encoder.Frames.Add(BitmapFrame.Create(origSource));
                                }
                            }

                            FileStream fs = File.Open(save.FileName, FileMode.Create);
                            encoder.Save(fs);
                            fs.Close();
                        }
                        else if (1 == save.FilterIndex)
                        {
                            TiffBitmapEncoder encoder = new TiffBitmapEncoder();

                            foreach (string path in files)
                            {
                                Uri myUri = new Uri(path, UriKind.RelativeOrAbsolute);
                                BmpBitmapDecoder decoder = new BmpBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.None);
                                BitmapSource origSource  = decoder.Frames[0];

                                BitmapMetadata bitmapMetadata  = new BitmapMetadata("tiff");
                                bitmapMetadata.ApplicationName = "Virtual Operation Panel";

                                if (null != origSource)
                                {
                                    encoder.Frames.Add(BitmapFrame.Create(origSource, null, bitmapMetadata, null));
                                }
                            }

                            FileStream fs = File.Open(save.FileName, FileMode.Create);
                            encoder.Save(fs);
                            fs.Close();
                        }
                        else if (2 == save.FilterIndex)
                        {
                            using (PdfHelper help = new PdfHelper())
                            {
                                help.Open(save.FileName);

                                foreach (string path in files)
                                {
                                    Uri myUri = new Uri(path, UriKind.RelativeOrAbsolute);
                                    BmpBitmapDecoder decoder = new BmpBitmapDecoder(myUri, BitmapCreateOptions.None, BitmapCacheOption.None);
                                    BitmapSource origSource  = decoder.Frames[0];

                                    if (null != origSource)
                                    {
                                        help.AddImage(origSource, 0);
                                    }
                                }

                                help.Close();
                            }
                        }
                    }
                    catch (Win32Exception)
                    {
                        fileSaveStatus = ScanFileSaveError.FileSave_OutOfMemory;
                    }
                    catch (COMException)
                    {
                        fileSaveStatus = ScanFileSaveError.FileSave_OutOfMemory;
                    }
                    catch (IOException)
                    {
                        fileSaveStatus = ScanFileSaveError.FileSave_FileOccupied;
                    }
                    catch
                    {
                        fileSaveStatus = ScanFileSaveError.FileSave_OutOfMemory;
                    }

                    CallbackMethod(null);
                });

                thread.SetApartmentState(ApartmentState.STA);
                thread.IsBackground = false;
                thread.Start();

                if (!thread.Join(100))
                {
                    pbw = new ProgressBarWindow();

                    pbw.Owner = App.Current.MainWindow;
                    pbw.ShowDialog();
                }

                thread.Join();

                if (fileSaveStatus == ScanFileSaveError.FileSave_OutOfMemory)
                {
                    VOP.Controls.MessageBoxEx.Show(
                        VOP.Controls.MessageBoxExStyle.Simple,
                        m_MainWin,
                        (string)this.FindResource("ResStr_Operation_cannot_be_carried_out_due_to_insufficient_memory_or_hard_disk_space_Please_try_again_after_freeing_memory_or_hard_disk_space_"),
                        (string)this.FindResource("ResStr_Error")
                        );
                }
                else if (fileSaveStatus == ScanFileSaveError.FileSave_FileOccupied)
                {
                    VOP.Controls.MessageBoxEx.Show(
                        VOP.Controls.MessageBoxExStyle.Simple,
                        m_MainWin,
                        (string)this.FindResource("ResStr_picture_file_occupied"),
                        (string)this.FindResource("ResStr_Warning")
                        );
                }
            }
        }