Image shape.
Inheritance: XText
Beispiel #1
0
 /// <summary>
 /// Initialize new instance of <see cref="ImageSelection"/> class.
 /// </summary>
 /// <param name="layer">The selection shapes layer.</param>
 /// <param name="shape">The selected shape.</param>
 /// <param name="style">The selection shapes style.</param>
 /// <param name="point">The selection point shape.</param>
 public ImageSelection(XLayer layer, XImage shape, ShapeStyle style, BaseShape point)
 {
     _layer = layer;
     _image = shape;
     _style = style;
     _point = point;
 }
Beispiel #2
0
        /// <inheritdoc/>
        public override async void LeftDown(double x, double y)
        {
            base.LeftDown(x, y);
            var editor = _serviceProvider.GetService<ProjectEditor>();
            double sx = editor.Project.Options.SnapToGrid ? ProjectEditor.Snap(x, editor.Project.Options.SnapX) : x;
            double sy = editor.Project.Options.SnapToGrid ? ProjectEditor.Snap(y, editor.Project.Options.SnapY) : y;
            switch (_currentState)
            {
                case ToolState.None:
                    {
                        if (editor.ImageImporter == null)
                            return;

                        var key = await editor.ImageImporter.GetImageKeyAsync();
                        if (key == null || string.IsNullOrEmpty(key))
                            return;

                        var style = editor.Project.CurrentStyleLibrary.Selected;
                        _image = XImage.Create(
                            sx, sy,
                            editor.Project.Options.CloneStyle ? style.Clone() : style,
                            editor.Project.Options.PointShape,
                            key);

                        var result = editor.TryToGetConnectionPoint(sx, sy);
                        if (result != null)
                        {
                            _image.TopLeft = result;
                        }

                        editor.Project.CurrentContainer.WorkingLayer.Shapes = editor.Project.CurrentContainer.WorkingLayer.Shapes.Add(_image);
                        editor.Project.CurrentContainer.WorkingLayer.Invalidate();
                        ToStateOne();
                        Move(_image);
                        _currentState = ToolState.One;
                        editor.CancelAvailable = true;
                    }
                    break;
                case ToolState.One:
                    {
                        if (_image != null)
                        {
                            _image.BottomRight.X = sx;
                            _image.BottomRight.Y = sy;

                            var result = editor.TryToGetConnectionPoint(sx, sy);
                            if (result != null)
                            {
                                _image.BottomRight = result;
                            }

                            editor.Project.CurrentContainer.WorkingLayer.Shapes = editor.Project.CurrentContainer.WorkingLayer.Shapes.Remove(_image);
                            Remove();
                            base.Finalize(_image);
                            editor.Project.AddShape(editor.Project.CurrentContainer.CurrentLayer, _image);
                            _currentState = ToolState.None;
                            editor.CancelAvailable = false;
                        }
                    }
                    break;
            }
        }
Beispiel #3
0
        /// <inheritdoc/>
        public override void Draw(object dc, XImage image, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            if (image.Key == null)
                return;

            var _dc = dc as DrawingContext;

            var style = image.Style;
            if (style == null)
                return;

            double thickness = style.Thickness / _state.ZoomX;
            double half = thickness / 2.0;

            Tuple<Brush, Pen> styleCached = _styleCache.Get(style);
            Brush fill;
            Pen stroke;
            if (styleCached != null)
            {
                fill = styleCached.Item1;
                stroke = styleCached.Item2;
            }
            else
            {
                fill = CreateBrush(style.Fill);
                stroke = CreatePen(style, thickness);
                _styleCache.Set(style, Tuple.Create(fill, stroke));
            }

            var rect = CreateRect(image.TopLeft, image.BottomRight, dx, dy);

            DrawRectangleInternal(_dc, half, fill, stroke, image.IsStroked, image.IsFilled, ref rect);

            var imageCached = _biCache.Get(image.Key);
            if (imageCached != null)
            {
                try
                {
                    _dc.DrawImage(imageCached, rect);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace);
                }
            }
            else
            {
                if (_state.ImageCache == null || string.IsNullOrEmpty(image.Key))
                    return;

                try
                {
                    var bytes = _state.ImageCache.GetImage(image.Key);
                    if (bytes != null)
                    {
                        var ms = new System.IO.MemoryStream(bytes);
                        var bi = new BitmapImage();
                        bi.BeginInit();
                        bi.StreamSource = ms;
                        bi.EndInit();
                        bi.Freeze();

                        _biCache.Set(image.Key, bi);

                        _dc.DrawImage(bi, rect);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace);
                }
            }
        }
Beispiel #4
0
        /// <inheritdoc/>
        public override void Draw(object dc, XImage image, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            var canvas = dc as SKCanvas;

            var rect = CreateRect(image.TopLeft, image.BottomRight, dx, dy, _scaleToPage);

            if (image.IsStroked || image.IsFilled)
            {
                using (SKPaint brush = ToSKPaintBrush(image.Style.Fill))
                using (SKPaint pen = ToSKPaintPen(image.Style, _scaleToPage, _sourceDpi, _targetDpi))
                {
                    DrawRectangleInternal(canvas, brush, pen, image.IsStroked, image.IsFilled, ref rect);
                }
            }

            if (_enableImageCache
                && _biCache.ContainsKey(image.Key))
            {
                canvas.DrawBitmap(_biCache[image.Key], rect);
            }
            else
            {
                if (State.ImageCache == null || string.IsNullOrEmpty(image.Key))
                    return;

                var bytes = State.ImageCache.GetImage(image.Key);
                if (bytes != null)
                {
                    var bi = SKBitmap.Decode(bytes);
                    if (_enableImageCache)
                        _biCache[image.Key] = bi;

                    canvas.DrawBitmap(bi, rect);

                    if (!_enableImageCache)
                        bi.Dispose();
                }
            }
        }
Beispiel #5
0
 /// <inheritdoc/>
 public override void Draw(object dc, XImage image, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
 {
     // TODO: Implement Draw image.
 }
Beispiel #6
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="image"></param>
        /// <param name="v"></param>
        /// <param name="threshold"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static BaseShape HitTestImage(XImage image, Vector2 v, double threshold, double dx, double dy)
        {
            if (ShapeBounds.GetPointBounds(image.TopLeft, threshold, dx, dy).Contains(v))
            {
                return image.TopLeft;
            }

            if (ShapeBounds.GetPointBounds(image.BottomRight, threshold, dx, dy).Contains(v))
            {
                return image.BottomRight;
            }

            if (ShapeBounds.GetImageBounds(image, dx, dy).Contains(v))
            {
                return image;
            }

            return null;
        }
Beispiel #7
0
 /// <summary>
 /// Draws a <see cref="XImage"/> shape using drawing context.
 /// </summary>
 /// <param name="dc">The native drawing context.</param>
 /// <param name="image">The <see cref="XImage"/> shape.</param>
 /// <param name="dx">The X coordinate offset.</param>
 /// <param name="dy">The Y coordinate offset.</param>
 /// <param name="db">The properties database.</param>
 /// <param name="r">The data record.</param>
 public abstract void Draw(object dc, XImage image, double dx, double dy, ImmutableArray<XProperty> db, XRecord r);
Beispiel #8
0
        /// <inheritdoc/>
        public override void Draw(object dc, XImage image, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            var _gfx = dc as Graphics;

            Brush brush = ToSolidBrush(image.Style.Stroke);

            var rect = CreateRect(
                image.TopLeft,
                image.BottomRight,
                dx, dy);

            var srect = new RectangleF(
                _scaleToPage(rect.X),
                _scaleToPage(rect.Y),
                _scaleToPage(rect.Width),
                _scaleToPage(rect.Height));

            if (image.IsFilled)
            {
                _gfx.FillRectangle(
                    ToSolidBrush(image.Style.Fill),
                    srect);
            }

            if (image.IsStroked)
            {
                _gfx.DrawRectangle(
                    ToPen(image.Style, _scaleToPage),
                    srect.X,
                    srect.Y,
                    srect.Width,
                    srect.Height);
            }

            if (_enableImageCache
                && _biCache.ContainsKey(image.Key))
            {
                _gfx.DrawImage(_biCache[image.Key], srect);
            }
            else
            {
                if (State.ImageCache == null || string.IsNullOrEmpty(image.Key))
                    return;

                var bytes = State.ImageCache.GetImage(image.Key);
                if (bytes != null)
                {
                    var ms = new System.IO.MemoryStream(bytes);
                    var bi = Image.FromStream(ms);
                    ms.Dispose();

                    if (_enableImageCache)
                        _biCache[image.Key] = bi;

                    _gfx.DrawImage(bi, srect);

                    if (!_enableImageCache)
                        bi.Dispose();
                }
            }

            brush.Dispose();
        }
Beispiel #9
0
 /// <summary>
 /// Get the bounding rectangle for <see cref="XImage"/> shape.
 /// </summary>
 /// <param name="image"></param>
 /// <param name="dx"></param>
 /// <param name="dy"></param>
 /// <returns></returns>
 public static Rect2 GetImageBounds(XImage image, double dx, double dy)
 {
     return Rect2.Create(image.TopLeft, image.BottomRight, dx, dy);
 }
Beispiel #10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="image"></param>
        /// <param name="rect"></param>
        /// <param name="selected"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static bool HitTestImage(XImage image, Rect2 rect, ISet<BaseShape> selected, double dx, double dy)
        {
            if (ShapeBounds.GetImageBounds(image, dx, dy).IntersectsWith(rect))
            {
                if (selected != null)
                {
                    selected.Add(image);
                    return false;
                }
                else
                {
                    return true;
                }
            }

            return false;
        }
Beispiel #11
-1
        /// <inheritdoc/>
        public override void Draw(object dc, XImage image, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            var _dc = dc as AM.DrawingContext;

            var rect = CreateRect(image.TopLeft, image.BottomRight, dx, dy);

            if (image.IsStroked || image.IsFilled)
            {
                AM.IBrush brush = ToBrush(image.Style.Fill);
                AM.Pen pen = ToPen(image.Style, _scaleToPage);

                DrawRectangleInternal(
                    _dc,
                    brush,
                    pen,
                    image.IsStroked,
                    image.IsFilled,
                    ref rect);
            }

            if (_enableImageCache
                && _biCache.ContainsKey(image.Key))
            {
                try
                {
                    var bi = _biCache[image.Key];
                    _dc.DrawImage(
                        bi,
                        1.0,
                        new A.Rect(0, 0, bi.PixelWidth, bi.PixelHeight),
                        new A.Rect(rect.X, rect.Y, rect.Width, rect.Height));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace);
                }
            }
            else
            {
                if (State.ImageCache == null || string.IsNullOrEmpty(image.Key))
                    return;

                try
                {
                    var bytes = State.ImageCache.GetImage(image.Key);
                    if (bytes != null)
                    {
                        using (var ms = new System.IO.MemoryStream(bytes))
                        {
                            var bi = new AMI.Bitmap(ms);

                            if (_enableImageCache)
                                _biCache[image.Key] = bi;

                            _dc.DrawImage(
                                bi,
                                1.0,
                                new A.Rect(0, 0, bi.PixelWidth, bi.PixelHeight),
                                new A.Rect(rect.X, rect.Y, rect.Width, rect.Height));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace);
                }
            }
        }