Exemplo n.º 1
0
        private void CopySelectedArea()
        {
            //clear copy-data cache
            if (this._img == null)
            {
                return;
            }

            var img = this._img;
            var tb  = this.ScaleImage(img);

            var bitmapRotate = ImageProcess.RotateImage(tb, img.RotateAngle);

            var rectRotate = new Rect();
            var newSize    = ImageProcess.RotateSize(img.ItemWidth, img.ItemHeight, img.RotateAngle);

            rectRotate = new Rect(
                (img.Left + img.ItemWidth / 2) - newSize.Width / 2,
                (img.Top + img.ItemHeight / 2) - newSize.Height / 2,
                newSize.Width,
                newSize.Height);

            var rectangleIntersect = GetIntersect(rectRotate);

            ///no intersect area
            if (rectangleIntersect == Rect.Empty)
            {
                return;
            }

            var source      = bitmapRotate;
            var orignalsize = new Size(source.Width, source.Height);

            var vmItem = MakeCopyImageWidget(source, orignalsize, rectangleIntersect, rectRotate);

            vmItem.Opacity = img.Opacity;

            img.IsSelected = false;
            _pageEditorViewModel.CopyWidget(vmItem, false);
            _pageEditorViewModel.DeleteItem(vmItem);
        }
Exemplo n.º 2
0
        private void CropAndCutSelectedImg()
        {
            if (this._img == null)
            {
                return;
            }

            var img = this._img;
            var tb  = this.ScaleImage(img);

            var bitmapRotate = ImageProcess.RotateImage(tb, img.RotateAngle);

            var rectRotate = new Rect();

            if (img.RotateAngle == 0d)
            {
                rectRotate = new Rect(img.Left, img.Top, img.ItemWidth, img.ItemHeight);
            }
            else
            {
                var newSize = ImageProcess.RotateSize(img.ItemWidth, img.ItemHeight, img.RotateAngle);
                rectRotate = new Rect(
                    (img.Left + img.ItemWidth / 2) - newSize.Width / 2,
                    (img.Top + img.ItemHeight / 2) - newSize.Height / 2,
                    newSize.Width,
                    newSize.Height);
            }

            var rectangleIntersect = GetIntersect(rectRotate);

            ///no intersect area
            if (rectangleIntersect == Rect.Empty)
            {
                return;
            }

            var source      = bitmapRotate;
            var orignalsize = new Size(source.Width, source.Height);

            ///to cut selected area
            var vmcopyItem = MakeCopyImageWidget(source, orignalsize, rectangleIntersect, rectRotate);

            vmcopyItem.IsSelected = false;
            _pageEditorViewModel.CopyWidget(vmcopyItem, true);
            _pageEditorViewModel.DeleteItem(vmcopyItem);

            ///rubber selected area
            var writablebitmap = new WriteableBitmap(source);
            var toRubberRect   = new Int32Rect(
                Convert.ToInt32((rectangleIntersect.Left - rectRotate.Left) * orignalsize.Width / rectRotate.Width),
                Convert.ToInt32((rectangleIntersect.Top - rectRotate.Top) * orignalsize.Height / rectRotate.Height),
                Convert.ToInt32(rectangleIntersect.Width * orignalsize.Width / rectRotate.Width),
                Convert.ToInt32(rectangleIntersect.Height * orignalsize.Height / rectRotate.Height));

            ImageProcess.Rubber(toRubberRect.X, toRubberRect.Y, toRubberRect.Width, toRubberRect.Height, writablebitmap);

            var ms      = new MemoryStream();
            var encoder = new PngBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(writablebitmap));
            encoder.Save(ms);
            ms.Seek(0, SeekOrigin.Begin);

            var widget = this._pageEditorViewModel.PageEditorModel.AddWidgetItem2Dom
                             (Naver.Compass.Service.Document.WidgetType.Image,
                             Naver.Compass.Service.Document.ShapeType.None,
                             rectRotate.Left,
                             rectRotate.Top,
                             Convert.ToInt32(rectRotate.Width),
                             Convert.ToInt32(rectRotate.Height));
            var vmItem = new ImageWidgetViewModel(widget);

            vmItem.IsSelected = true;
            vmItem.ImportImgSeekLater(ms, false);
            vmItem.Opacity = img.Opacity;

            ///replace selected widget with cropped widget
            img.IsSelected = false;
            this._pageEditorViewModel.ReplaceWidget(
                new List <WidgetViewModBase> {
                img
            },
                new List <WidgetViewModBase> {
                vmItem
            });
        }
Exemplo n.º 3
0
        private void SliceSelectedImage(System.Windows.Point slicePoint)
        {
            if (this._img == null)
            {
                return;
            }

            var img = this._img;
            var tb  = this.ScaleImage(img);

            var bitmapRotate = ImageProcess.RotateImage(tb, img.RotateAngle);

            var rectRotate = new Rect();

            if (img.RotateAngle == 0d)
            {
                rectRotate = new Rect(img.Left, img.Top, img.ItemWidth, img.ItemHeight);
            }
            else
            {
                var newSize = ImageProcess.RotateSize(img.ItemWidth, img.ItemHeight, img.RotateAngle);
                rectRotate = new Rect(
                    (img.Left + img.ItemWidth / 2) - newSize.Width / 2,
                    (img.Top + img.ItemHeight / 2) - newSize.Height / 2,
                    newSize.Width,
                    newSize.Height);
            }

            var rectangles = new List <Rect>();

            ///      |            |
            ///   1  |      2     |   3
            /// _____|____________|_______
            ///   4  |      5     |   6
            /// _____|____________|_______
            ///   7  |      8     |   9
            ///      |            |

            /// in rect 1,3,7,9
            if ((slicePoint.X <= rectRotate.Left && slicePoint.Y <= rectRotate.Top) ||
                (slicePoint.X >= rectRotate.Left + rectRotate.Width && slicePoint.Y <= rectRotate.Top) ||
                (slicePoint.X <= rectRotate.Left && slicePoint.Y >= rectRotate.Top + rectRotate.Height) ||
                (slicePoint.X >= rectRotate.Left + rectRotate.Width && slicePoint.Y >= rectRotate.Top + rectRotate.Height))
            {
                return;
            }

            /// in rect 2,8
            if ((slicePoint.X > rectRotate.Left && slicePoint.X < rectRotate.Left + rectRotate.Width) &&
                (slicePoint.Y <= rectRotate.Top || slicePoint.Y >= rectRotate.Top + rectRotate.Height))
            {
                if (SliceType != Module.SliceType.Horizontal)
                {
                    rectangles.Add(new Rect(0, 0, slicePoint.X - rectRotate.Left, rectRotate.Height));
                    rectangles.Add(new Rect(slicePoint.X - rectRotate.Left, 0, rectRotate.Width - (slicePoint.X - rectRotate.Left), rectRotate.Height));
                }
            }

            /// in rect 4,6
            if ((slicePoint.X <= rectRotate.Left || slicePoint.X >= rectRotate.Left + rectRotate.Width) &&
                (slicePoint.Y > rectRotate.Top && slicePoint.Y < rectRotate.Top + rectRotate.Height))
            {
                if (SliceType != Module.SliceType.Vertical)
                {
                    rectangles.Add(new Rect(0, 0, rectRotate.Width, slicePoint.Y - rectRotate.Top));
                    rectangles.Add(new Rect(0, slicePoint.Y - rectRotate.Top, rectRotate.Width, rectRotate.Height - (slicePoint.Y - rectRotate.Top)));
                }
            }

            /// in rect 5
            if ((slicePoint.X > rectRotate.Left && slicePoint.X < rectRotate.Left + rectRotate.Width) &&
                (slicePoint.Y > rectRotate.Top && slicePoint.Y < rectRotate.Top + rectRotate.Height))
            {
                if (SliceType == Module.SliceType.Vertical)
                {
                    rectangles.Add(new Rect(0, 0, slicePoint.X - rectRotate.Left, rectRotate.Height));
                    rectangles.Add(new Rect(slicePoint.X - rectRotate.Left, 0, rectRotate.Width - (slicePoint.X - rectRotate.Left), rectRotate.Height));
                }
                else if (SliceType == Module.SliceType.Horizontal)
                {
                    rectangles.Add(new Rect(0, 0, rectRotate.Width, slicePoint.Y - rectRotate.Top));
                    rectangles.Add(new Rect(0, slicePoint.Y - rectRotate.Top, rectRotate.Width, rectRotate.Height - (slicePoint.Y - rectRotate.Top)));
                }
                else if (SliceType == Module.SliceType.Cross)
                {
                    rectangles.Add(new Rect(0, 0, slicePoint.X - rectRotate.Left, slicePoint.Y - rectRotate.Top));
                    rectangles.Add(new Rect(slicePoint.X - rectRotate.Left, 0, rectRotate.Width - (slicePoint.X - rectRotate.Left), slicePoint.Y - rectRotate.Top));
                    rectangles.Add(new Rect(0, slicePoint.Y - rectRotate.Top, slicePoint.X - rectRotate.Left, rectRotate.Height - (slicePoint.Y - rectRotate.Top)));
                    rectangles.Add(new Rect(slicePoint.X - rectRotate.Left, slicePoint.Y - rectRotate.Top, rectRotate.Width - (slicePoint.X - rectRotate.Left), rectRotate.Height - (slicePoint.Y - rectRotate.Top)));
                }
            }

            if (rectangles.Count > 0)
            {
                var source      = bitmapRotate;
                var orignalsize = new Size(source.Width, source.Height);
                var newWidgets  = new List <WidgetViewModBase>();
                foreach (var rect in rectangles)
                {
                    var widget = this._pageEditorViewModel.PageEditorModel.AddWidgetItem2Dom
                                     (Naver.Compass.Service.Document.WidgetType.Image,
                                     Naver.Compass.Service.Document.ShapeType.None,
                                     rectRotate.Left + rect.X,
                                     rectRotate.Top + rect.Y,
                                     Convert.ToInt32(rect.Width),
                                     Convert.ToInt32(rect.Height));
                    var vmItem = new ImageWidgetViewModel(widget);
                    vmItem.IsSelected = true;
                    var cb = new CroppedBitmap(
                        source,
                        new Int32Rect(
                            Convert.ToInt32(rect.X * orignalsize.Width / rectRotate.Width),
                            Convert.ToInt32(rect.Y * orignalsize.Height / rectRotate.Height),
                            Convert.ToInt32(rect.Width * orignalsize.Width / rectRotate.Width),
                            Convert.ToInt32(rect.Height * orignalsize.Height / rectRotate.Height)));
                    var ms      = new MemoryStream();
                    var encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(cb));
                    encoder.Save(ms);
                    ms.Seek(0, SeekOrigin.Begin);
                    vmItem.ImportImgSeekLater(ms, false);
                    vmItem.Opacity = img.Opacity;
                    newWidgets.Add(vmItem);
                }
                img.IsSelected = false;
                this._pageEditorViewModel.ReplaceWidget(new List <WidgetViewModBase> {
                    img
                }, newWidgets);
            }
        }