Пример #1
0
        private static void SaveCanvasAsImg()
        {
            var path = DialogService.Current.GetSaveFilePath(SaveAsImg_DefaultFileName);

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            try {
                using (var fs = File.Create(path)) {
                    CanvasService.CanvasDataContext.CommitEdit();

                    var drawObjects = CanvasService.CanvasDataContext.GetAllVisibleDrawObjects();
                    var setting     = new ExportImgSetting(fs);
                    ServiceProvider.GetInstance <IExportCanvasAsImgService>().ExportDrawObjectsAsImg(drawObjects, setting);
                }

                if (MsgBoxService.Show(
                        LanguageService.FindResourceString(
                            MsgText_ConfirmToShowExportedDirectory
                            ),
                        MessageBoxButton.YesNo
                        ) == MessageBoxResult.Yes)
                {
                    LocalExplorerService.OpenFolderAndSelectFile(path);
                }
            }
            catch (Exception ex) {
                LoggerService.WriteException(ex);
                MsgBoxService.Show(ex.Message);
            }
        }
        internal static IDelegateCommand CreateCopyToNewFileCommand(IHexDataContext hexDataContext)
        {
            var copyToNewFileCommand = CommandFactory.CreateDelegateCommand(
                () => {
                if (hexDataContext.SelectionLength == -1 || hexDataContext.SelectionStart == -1)
                {
                    return;
                }

                var dialogService = ServiceProvider.Current.GetInstance <IDialogService>();
                if (dialogService == null)
                {
                    LoggerService.WriteCallerLine($"{nameof(dialogService)} can't be null.");
                    return;
                }

                var path = dialogService.GetSaveFilePath();
                if (path == null)
                {
                    return;
                }

                var dialog         = dialogService.CreateLoadingDialog();
                dialog.WindowTitle = LanguageService.FindResourceString(Constants.WindowTitle_DataCopying);
                dialog.DoWork     += (sender, e) => {
                    FileStream fs = null;
                    InterceptStream sourceStream = null;

                    try {
                        fs            = File.Create(path);
                        byte[] buffer = new byte[10485760];
                        int read;
                        long readSize = 0;
                        hexDataContext.Stream.Position = hexDataContext.SelectionStart;

                        sourceStream = InterceptStream.CreateFromStream(
                            hexDataContext.Stream,
                            hexDataContext.SelectionStart,
                            hexDataContext.SelectionLength);

                        while ((read = sourceStream.Read(buffer, 0, buffer.Length)) != 0 && !dialog.CancellationPending)
                        {
                            fs.Write(buffer, 0, read);
                            readSize += read;
                            dialog.ReportProgress((int)(readSize * 100 / sourceStream.Length));
                        }
                    }
                    catch (Exception ex) {
                        LoggerService.WriteCallerLine(ex.Message);
                    }
                    finally {
                        fs?.Dispose();
                        sourceStream?.Dispose();
                    }
                };

                dialog.RunWorkerCompleted += (sender, e) => {
                    if (e.Cancelled)
                    {
                        return;
                    }

                    LocalExplorerService.OpenFolderAndSelectFile(path);
                    MsgBoxService.Current?.Show(LanguageService.Current?.FindResourceString(Constants.WindowTitle_DataCopyDone));
                };

                dialog.ShowDialog();
            }, () => SelectionAvailable(hexDataContext));

            hexDataContext.SelectionStateChanged += delegate {
                copyToNewFileCommand.RaiseCanExecuteChanged();
            };
            return(copyToNewFileCommand);
        }