示例#1
0
        //
        public void ClearImg(bool pushToUndoStack = true)
        {
            PropertyMementos mementos = CreateNewPropertyMementos();

            mementos.AddPropertyMemento(new PropertyMemento("ItemHeight", ItemHeight, ItemHeight));
            mementos.AddPropertyMemento(new PropertyMemento("ItemWidth", ItemWidth, ItemWidth));
            mementos.AddPropertyMemento(new PropertyMemento("Opacity", Opacity, Opacity));
            ClearImageCommand imgCmd = new ClearImageCommand(this, _imageStream, _isAutosize);

            //reader = File.OpenRead(openFile.FileName);

            _nailStream  = null;
            _imageStream = null;
            (_model as ImageModel).ImageStream = _imageStream;
            BackgroundShow = Visibility.Visible;
            Opacity        = 1;
            FirePropertyChanged("ImgSource");
            CreateNailStreamAndShow();
            if (pushToUndoStack && CurrentUndoManager != null)
            {
                mementos.SetPropertyNewValue("ItemHeight", ItemHeight);
                mementos.SetPropertyNewValue("ItemWidth", ItemWidth);
                mementos.SetPropertyNewValue("Opacity", Opacity);
                PropertyChangeCommand propCmd = new PropertyChangeCommand(this, mementos);

                UndoCompositeCommand cmds = new UndoCompositeCommand();
                cmds.AddCommand(imgCmd);
                cmds.AddCommand(propCmd);

                cmds.DeselectAllWidgetsFirst();
                CurrentUndoManager.Push(cmds);
            }
        }
示例#2
0
        private void CreateImageFromPath(string path)
        {
            Stream oldStream;

            if (_imageStream != null)
            {
                oldStream = new MemoryStream((int)_imageStream.Length);
                _imageStream.Seek(0, SeekOrigin.Begin);
                _imageStream.CopyTo(oldStream);
                oldStream.Seek(0, SeekOrigin.Begin);
            }
            else
            {
                oldStream = null;
            }

            bool             isAutosizeOldValue = _isAutosize;
            PropertyMementos mementos           = CreateNewPropertyMementos();

            mementos.AddPropertyMemento(new PropertyMemento("ItemHeight", ItemHeight, ItemHeight));
            mementos.AddPropertyMemento(new PropertyMemento("ItemWidth", ItemWidth, ItemWidth));

            _isAutosize = true;

            try
            {
                //reader = File.OpenRead(openFile.FileName);
                _imageStream = new MemoryStream(File.ReadAllBytes(path));
                (_model as ImageModel).ImageStream = _imageStream;

                // Get ImgSource so that ItemHeight and ItemWidth have the actual size value of the new image.
                // Here, FirePropertyChanged("ImgSource"); will not go into ImgSource get method when dragging a image file
                // on canvas, this cause hight and width are not updated to actual image size.
                // FirePropertyChanged("ImgSource") works well when importing image via file dialog, I don't know why
                // it doesn't work when dragging.
                // This is a workaround, remove this when you figure out the root cause.
                // ImageSource temp = ImgSource;

                FirePropertyChanged("ImgSource");

                if (CurrentUndoManager != null)
                {
                    mementos.SetPropertyNewValue("ItemHeight", ItemHeight);
                    mementos.SetPropertyNewValue("ItemWidth", ItemWidth);

                    PropertyChangeCommand propCmd = new PropertyChangeCommand(this, mementos);
                    ImportImageCommand    imgCmd  = new ImportImageCommand(this, oldStream, _imageStream, isAutosizeOldValue, _isAutosize);

                    UndoCompositeCommand cmds = new UndoCompositeCommand();
                    cmds.AddCommand(imgCmd);
                    cmds.AddCommand(propCmd);

                    cmds.DeselectAllWidgetsFirst();
                    CurrentUndoManager.Push(cmds);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, GlobalData.FindResource("Common_Error"), MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }