Пример #1
0
        protected override HistoryMemento OnUndo(ProgressEventHandler progressCallback)
        {
            BitmapHistoryMementoData data = base.Data as BitmapHistoryMementoData;
            BitmapLayer layer             = (BitmapLayer)this.historyWorkspace.Document.Layers[this.layerIndex];

            RectInt32[]          savedRegion = data.SavedRegion;
            MaskedSurface        surface     = null;
            BitmapHistoryMemento memento     = new BitmapHistoryMemento(base.Name, base.Image, this.historyWorkspace, this.layerIndex, savedRegion);

            if (surface != null)
            {
                surface.Draw(layer.Surface);
            }
            else
            {
                using (FileStream stream = FileSystem.OpenStreamingFile(this.tempFileName, FileAccess.Read))
                {
                    LoadSurfaceRegion(stream, layer.Surface, data.SavedRegion);
                }
                this.tempFileHandle.Dispose();
                this.tempFileHandle = null;
            }
            if (savedRegion.Length != 0)
            {
                RectInt32 roi = savedRegion.Bounds();
                layer.Invalidate(roi);
            }
            return(memento);
        }
Пример #2
0
        protected override HistoryMemento OnUndo()
        {
            BitmapHistoryMementoData data = this.Data as BitmapHistoryMementoData;
            BitmapLayer layer             = (BitmapLayer)this.historyWorkspace.Document.Layers[this.layerIndex];

            PdnRegion     region;
            MaskedSurface maskedSurface = null;

            if (this.poMaskedSurfaceRef != Guid.Empty)
            {
                PersistedObject <MaskedSurface> poMS = PersistedObjectLocker.Get <MaskedSurface>(this.poMaskedSurfaceRef);
                maskedSurface = poMS.Object;
                region        = maskedSurface.CreateRegion();
            }
            else if (data.UndoImage == null)
            {
                region = data.SavedRegion;
            }
            else
            {
                region = data.UndoImage.Region;
            }

            BitmapHistoryMemento redo;

            if (this.poUndoMaskedSurfaceRef == Guid.Empty)
            {
                redo = new BitmapHistoryMemento(Name, Image, this.historyWorkspace, this.layerIndex, region);
                redo.poUndoMaskedSurfaceRef = this.poMaskedSurfaceRef;
            }
            else
            {
                redo = new BitmapHistoryMemento(Name, Image, this.historyWorkspace, this.layerIndex, this.poUndoMaskedSurfaceRef);
            }

            PdnRegion simplified = Utility.SimplifyAndInflateRegion(region);

            if (maskedSurface != null)
            {
                maskedSurface.Draw(layer.Surface);
            }
            else if (data.UndoImage == null)
            {
                using (FileStream input = FileSystem.OpenStreamingFile(this.tempFileName, FileAccess.Read))
                {
                    LoadSurfaceRegion(input, layer.Surface, data.SavedRegion);
                }

                data.SavedRegion.Dispose();
                this.tempFileHandle.Dispose();
                this.tempFileHandle = null;
            }
            else
            {
                data.UndoImage.Draw(layer.Surface);
                data.UndoImage.Dispose();
            }

            layer.Invalidate(simplified);
            simplified.Dispose();

            return(redo);
        }
        public bool PerformAction()
        {
            bool success = true;

            if (this.documentWorkspace.Selection.IsEmpty ||
                !(this.documentWorkspace.ActiveLayer is BitmapLayer))
            {
                return false;
            }

            try
            {
                using (new WaitCursorChanger(this.documentWorkspace))
                {
                    Utility.GCFullCollect();
                    PdnRegion selectionRegion = this.documentWorkspace.Selection.CreateRegion();
                    PdnGraphicsPath selectionOutline = this.documentWorkspace.Selection.CreatePath();
                    BitmapLayer activeLayer = (BitmapLayer)this.documentWorkspace.ActiveLayer;
                    RenderArgs renderArgs = new RenderArgs(activeLayer.Surface);
                    MaskedSurface maskedSurface = new MaskedSurface(renderArgs.Surface, selectionOutline);
                    SurfaceForClipboard surfaceForClipboard = new SurfaceForClipboard(maskedSurface);
                    Rectangle selectionBounds = Utility.GetRegionBounds(selectionRegion);

                    if (selectionBounds.Width > 0 && selectionBounds.Height > 0)
                    {
                        Surface copySurface = new Surface(selectionBounds.Width, selectionBounds.Height);
                        Bitmap copyBitmap = copySurface.CreateAliasedBitmap();
                        Bitmap copyOpaqueBitmap = new Bitmap(copySurface.Width, copySurface.Height, PixelFormat.Format24bppRgb);

                        using (Graphics copyBitmapGraphics = Graphics.FromImage(copyBitmap))
                        {
                            copyBitmapGraphics.Clear(Color.White);
                        }

                        maskedSurface.Draw(copySurface, -selectionBounds.X, -selectionBounds.Y);

                        using (Graphics copyOpaqueBitmapGraphics = Graphics.FromImage(copyOpaqueBitmap))
                        {
                            copyOpaqueBitmapGraphics.Clear(Color.White);
                            copyOpaqueBitmapGraphics.DrawImage(copyBitmap, 0, 0);
                        }

                        DataObject dataObject = new DataObject();

                        dataObject.SetData(DataFormats.Bitmap, copyOpaqueBitmap);
                        dataObject.SetData(surfaceForClipboard);

                        int retryCount = 2;

                        while (retryCount >= 0)
                        {
                            try
                            {
                                using (new WaitCursorChanger(this.documentWorkspace))
                                {
                                    Clipboard.SetDataObject(dataObject, true);
                                }

                                break;
                            }

                            catch
                            {
                                if (retryCount == 0)
                                {
                                    success = false;
                                    Utility.ErrorBox(this.documentWorkspace,
                                        PdnResources.GetString("CopyAction.Error.TransferToClipboard"));
                                }
                                else
                                {
                                    Thread.Sleep(200);
                                }
                            }

                            finally
                            {
                                --retryCount;
                            }
                        }

                        copySurface.Dispose();
                        copyBitmap.Dispose();
                        copyOpaqueBitmap.Dispose();
                    }

                    selectionRegion.Dispose();
                    selectionOutline.Dispose();
                    renderArgs.Dispose();
                    maskedSurface.Dispose();
                }
            }

            catch (OutOfMemoryException)
            {
                success = false;
                Utility.ErrorBox(this.documentWorkspace, PdnResources.GetString("CopyAction.Error.OutOfMemory"));
            }

            catch (Exception)
            {
                success = false;
                Utility.ErrorBox(this.documentWorkspace, PdnResources.GetString("CopyAction.Error.Generic"));
            }

            Utility.GCFullCollect();
            return success;
        }
        public bool PerformAction()
        {
            bool success = true;

            if (this.documentWorkspace.Selection.IsEmpty ||
                !(this.documentWorkspace.ActiveLayer is BitmapLayer))
            {
                return(false);
            }

            try
            {
                using (new WaitCursorChanger(this.documentWorkspace))
                {
                    Utility.GCFullCollect();
                    PdnRegion           selectionRegion     = this.documentWorkspace.Selection.CreateRegion();
                    PdnGraphicsPath     selectionOutline    = this.documentWorkspace.Selection.CreatePath();
                    BitmapLayer         activeLayer         = (BitmapLayer)this.documentWorkspace.ActiveLayer;
                    RenderArgs          renderArgs          = new RenderArgs(activeLayer.Surface);
                    MaskedSurface       maskedSurface       = new MaskedSurface(renderArgs.Surface, selectionOutline);
                    SurfaceForClipboard surfaceForClipboard = new SurfaceForClipboard(maskedSurface);
                    Rectangle           selectionBounds     = Utility.GetRegionBounds(selectionRegion);

                    if (selectionBounds.Width > 0 && selectionBounds.Height > 0)
                    {
                        Surface copySurface      = new Surface(selectionBounds.Width, selectionBounds.Height);
                        Bitmap  copyBitmap       = copySurface.CreateAliasedBitmap();
                        Bitmap  copyOpaqueBitmap = new Bitmap(copySurface.Width, copySurface.Height, PixelFormat.Format24bppRgb);

                        using (Graphics copyBitmapGraphics = Graphics.FromImage(copyBitmap))
                        {
                            copyBitmapGraphics.Clear(Color.White);
                        }

                        maskedSurface.Draw(copySurface, -selectionBounds.X, -selectionBounds.Y);

                        using (Graphics copyOpaqueBitmapGraphics = Graphics.FromImage(copyOpaqueBitmap))
                        {
                            copyOpaqueBitmapGraphics.Clear(Color.White);
                            copyOpaqueBitmapGraphics.DrawImage(copyBitmap, 0, 0);
                        }

                        DataObject dataObject = new DataObject();

                        dataObject.SetData(DataFormats.Bitmap, copyOpaqueBitmap);
                        dataObject.SetData(surfaceForClipboard);

                        int retryCount = 2;

                        while (retryCount >= 0)
                        {
                            try
                            {
                                using (new WaitCursorChanger(this.documentWorkspace))
                                {
                                    Clipboard.SetDataObject(dataObject, true);
                                }

                                break;
                            }

                            catch
                            {
                                if (retryCount == 0)
                                {
                                    success = false;
                                    Utility.ErrorBox(this.documentWorkspace,
                                                     PdnResources.GetString("CopyAction.Error.TransferToClipboard"));
                                }
                                else
                                {
                                    Thread.Sleep(200);
                                }
                            }

                            finally
                            {
                                --retryCount;
                            }
                        }

                        copySurface.Dispose();
                        copyBitmap.Dispose();
                        copyOpaqueBitmap.Dispose();
                    }

                    selectionRegion.Dispose();
                    selectionOutline.Dispose();
                    renderArgs.Dispose();
                    maskedSurface.Dispose();
                }
            }

            catch (OutOfMemoryException)
            {
                success = false;
                Utility.ErrorBox(this.documentWorkspace, PdnResources.GetString("CopyAction.Error.OutOfMemory"));
            }

            catch (Exception)
            {
                success = false;
                Utility.ErrorBox(this.documentWorkspace, PdnResources.GetString("CopyAction.Error.Generic"));
            }

            Utility.GCFullCollect();
            return(success);
        }