Bloom.Edit.EditingModel.ChangePicture C# (CSharp) Method

ChangePicture() public method

public ChangePicture ( GeckoHtmlElement img, Palaso.UI.WindowsForms.ImageToolbox.PalasoImage imageInfo, IProgress progress ) : void
img GeckoHtmlElement
imageInfo Palaso.UI.WindowsForms.ImageToolbox.PalasoImage
progress IProgress
return void
        public void ChangePicture(GeckoHtmlElement img, PalasoImage imageInfo, IProgress progress)
        {
            try
            {
                Logger.WriteMinorEvent("Starting ChangePicture {0}...", imageInfo.FileName);
                var editor = new PageEditingModel();
                editor.ChangePicture(CurrentBook.FolderPath, new ElementProxy(img), imageInfo, progress);

                // We need to save so that when asked by the thumbnailer, the book will give the proper image
                SaveNow();

                // BL-3717: if we cleanup unused image files whenever we change a picture then Cut can lose
                // all of an image's metadata (because the actual file is missing from the book folder when we go to
                // paste in the image that was copied onto the clipboard, which doesn't have metadata.)
                // Let's only do this on ExpensiveIntialization() when loading a book.
                //CurrentBook.Storage.CleanupUnusedImageFiles();

                // But after saving, we need the non-cleaned version back there
                _view.UpdateSingleDisplayedPage(_pageSelection.CurrentSelection);

                _view.UpdateThumbnailAsync(_pageSelection.CurrentSelection);
                Logger.WriteMinorEvent("Finished ChangePicture {0} (except for async thumbnail) ...", imageInfo.FileName);
                Analytics.Track("Change Picture");
                Logger.WriteEvent("ChangePicture {0}...", imageInfo.FileName);

            }
            catch (Exception e)
            {
                var msg = LocalizationManager.GetString("Errors.ProblemImportingPicture","Bloom had a problem importing this picture.");
                ErrorReport.NotifyUserOfProblem(e, msg+Environment.NewLine+e.Message);
            }
        }

Usage Example

Example #1
0
        private void OnPasteImage(DomEventArgs ge)
        {
            if (!_model.CanChangeImages())
            {
                MessageBox.Show(
                    LocalizationManager.GetString("EditTab.CantPasteImageLocked", "Sorry, this book is locked down so that images cannot be changed."));
                return;
            }

            Image clipboardImage = null;

            try
            {
                clipboardImage = GetImageFromClipboard();
                if (clipboardImage == null)
                {
                    MessageBox.Show(
                        LocalizationManager.GetString("EditTab.NoImageFoundOnClipboard", "Before you can paste an image, copy one onto your 'clipboard', from another program."));
                    return;
                }

                var target = (GeckoHtmlElement)ge.Target.CastToGeckoElement();
                if (target.ClassName.Contains("licenseImage"))
                {
                    return;
                }

                var imageElement = GetImageNode(ge);
                if (imageElement == null)
                {
                    return;
                }
                Cursor = Cursors.WaitCursor;

                //nb: later, code closer to the the actual book folder will
                //improve this file name. Taglib# requires an extension that matches the file content type, however.
                using (var temp = TempFile.WithExtension("png"))
                {
                    clipboardImage.Save(temp.Path, ImageFormat.Png);
//                    using (var progressDialog = new ProgressDialogBackground())
//                    {
//                        progressDialog.ShowAndDoWork((progress, args) =>
//                                                         {
//                                                             ImageUpdater.CompressImage(temp.Path, progress);
//                                                         });
//                    }
                    using (var palasoImage = PalasoImage.FromFile(temp.Path))
                    {
                        _model.ChangePicture(imageElement, palasoImage, new NullProgress());
                    }
                }
            }
            catch (Exception error)
            {
                Palaso.Reporting.ErrorReport.NotifyUserOfProblem(error, "The program had trouble getting an image from the clipboard.");
            }
            finally
            {
                if (clipboardImage != null)
                {
                    clipboardImage.Dispose();
                }
            }
            Cursor = Cursors.Default;
        }