/// <summary> /// /// </summary> /// <param name="image"></param> /// <param name="x"></param> /// <param name="y"></param> public void TryToConnectBottomRight(XImage image, double x, double y) { var result = ShapeBounds.HitTest(_editor.Project.CurrentContainer, new Vector2(x, y), _editor.Project.Options.HitTreshold); if (result != null && result is XPoint) { image.BottomRight = result as XPoint; } }
/// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="y"></param> public override async void LeftDown(double x, double y) { double sx = _editor.Project.Options.SnapToGrid ? Editor.Snap(x, _editor.Project.Options.SnapX) : x; double sy = _editor.Project.Options.SnapToGrid ? Editor.Snap(y, _editor.Project.Options.SnapY) : y; switch (_currentState) { case State.None: { if (_editor.GetImageKey == null) return; var path = await _editor.GetImageKey(); if (path == null || string.IsNullOrEmpty(path)) return; _shape = XImage.Create( sx, sy, _editor.Project.CurrentStyleLibrary.CurrentStyle, _editor.Project.Options.PointShape, path); if (_editor.Project.Options.TryToConnect) { TryToConnectTopLeft(_shape as XImage, sx, sy); } _editor.Project.CurrentContainer.WorkingLayer.Shapes = _editor.Project.CurrentContainer.WorkingLayer.Shapes.Add(_shape); _editor.Project.CurrentContainer.WorkingLayer.Invalidate(); ToStateOne(); Move(_shape); _editor.Project.CurrentContainer.HelperLayer.Invalidate(); _currentState = State.One; _editor.CancelAvailable = true; } break; case State.One: { var image = _shape as XImage; if (image != null) { image.BottomRight.X = sx; image.BottomRight.Y = sy; if (_editor.Project.Options.TryToConnect) { TryToConnectBottomRight(_shape as XImage, sx, sy); } _editor.Project.CurrentContainer.WorkingLayer.Shapes = _editor.Project.CurrentContainer.WorkingLayer.Shapes.Remove(_shape); Remove(); Finalize(_shape); _editor.AddWithHistory(_shape); _currentState = State.None; _editor.CancelAvailable = false; } } break; } }
/// <summary> /// /// </summary> /// <param name="gfx"></param> /// <param name="image"></param> /// <param name="dx"></param> /// <param name="dy"></param> /// <param name="db"></param> /// <param name="r"></param> public void Draw(object gfx, XImage image, double dx, double dy, ImmutableArray<ShapeProperty> db, Record r) { var _gfx = gfx 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.Path)) { _gfx.DrawImage(_biCache[image.Path], srect); } else { if (_state.ImageCache == null || string.IsNullOrEmpty(image.Path)) return; var bytes = _state.ImageCache.GetImage(image.Path); if (bytes != null) { var ms = new System.IO.MemoryStream(bytes); var bi = Image.FromStream(ms); ms.Dispose(); if (_enableImageCache) _biCache[image.Path] = bi; _gfx.DrawImage(bi, srect); if (!_enableImageCache) bi.Dispose(); } } brush.Dispose(); }
public void Draw(object ds, XImage image, double dx, double dy, ImmutableArray<ShapeProperty> db, Record r) { var _ds = ds as CanvasDrawingSession; var rect = CreateRect( image.TopLeft, image.BottomRight, dx, dy); if (image.IsFilled || image.IsStroked) { double thickness = image.Style.Thickness / _state.Zoom; var brush = ToColor(image.Style.Fill); var pen = ToColor(image.Style.Stroke); var ss = CreateStrokeStyle(image.Style); DrawRectangleInternal(_ds, brush, pen, ss, image.IsStroked, image.IsFilled, ref rect, thickness); ss.Dispose(); } var srect = new Rect(rect.X, rect.Y, rect.Width, rect.Height); if (_enableImageCache && _biCache.ContainsKey(image.Path)) { _ds.DrawImage(_biCache[image.Path], srect); } else { // TODO: Image caching is done in MainPage because calls to GetResults() throw exception. /* if (_state.ImageCache == null || string.IsNullOrEmpty(image.Path)) return; var bytes = _state.ImageCache.GetImage(image.Path); if (bytes != null) { var ms = new MemoryStream(bytes); var ras = ms.AsRandomAccessStream(); var bi = CanvasBitmap.LoadAsync(_ds, ras).GetResults(); ras.Dispose(); ms.Dispose(); if (_enableImageCache) _biCache[image.Path] = bi; _ds.DrawImage(bi, srect); if (!_enableImageCache) bi.Dispose(); } */ } }
/// <summary> /// /// </summary> /// <param name="dc"></param> /// <param name="image"></param> /// <param name="dx"></param> /// <param name="dy"></param> /// <param name="db"></param> /// <param name="r"></param> public void Draw(object dc, XImage image, double dx, double dy, ImmutableArray<ShapeProperty> db, Record r) { var _dc = dc as IDrawingContext; var rect = CreateRect( image.TopLeft, image.BottomRight, dx, dy); if (image.IsStroked || image.IsFilled) { Brush brush = ToSolidBrush(image.Style.Fill); Pen pen = ToPen(image.Style, _scaleToPage); DrawRectangleInternal( _dc, brush, pen, image.IsStroked, image.IsFilled, ref rect); // TODO: brush.Dispose(); // TODO: pen.Dispose(); } if (_enableImageCache && _biCache.ContainsKey(image.Path)) { var bi = _biCache[image.Path]; _dc.DrawImage( bi, 1.0, new Rect(0, 0, bi.PixelWidth, bi.PixelHeight), new Rect(rect.X, rect.Y, rect.Width, rect.Height)); } else { if (_state.ImageCache == null || string.IsNullOrEmpty(image.Path)) return; var bytes = _state.ImageCache.GetImage(image.Path); if (bytes != null) { using (var ms = new System.IO.MemoryStream(bytes)) { var bi = new Bitmap(ms); if (_enableImageCache) _biCache[image.Path] = bi; _dc.DrawImage( bi, 1.0, new Rect(0, 0, bi.PixelWidth, bi.PixelHeight), new Rect(rect.X, rect.Y, rect.Width, rect.Height)); // TODO: //if (!_enableImageCache) // bi.Dispose(); } } } }
/// <summary> /// /// </summary> /// <param name="dc"></param> /// <param name="image"></param> /// <param name="dx"></param> /// <param name="dy"></param> /// <param name="db"></param> /// <param name="r"></param> public void Draw(object dc, XImage image, double dx, double dy, ImmutableArray<ShapeProperty> db, Record r) { if (image.Path == null) return; var _dc = dc as DrawingContext; var style = image.Style; if (style == null) return; double thickness = style.Thickness / _state.Zoom; double half = thickness / 2.0; Tuple<Brush, Pen> cache = null; Brush fill; Pen stroke; if (_enableStyleCache && _styleCache.TryGetValue(style, out cache)) { fill = cache.Item1; stroke = cache.Item2; } else { fill = CreateBrush(style.Fill); stroke = CreatePen(style, thickness); if (_enableStyleCache) _styleCache.Add(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); if (_enableImageCache && _biCache.ContainsKey(image.Path)) { try { _dc.DrawImage(_biCache[image.Path], rect); } catch (Exception ex) { Debug.Print(ex.Message); Debug.Print(ex.StackTrace); } } else { if (_state.ImageCache == null || string.IsNullOrEmpty(image.Path)) return; try { var bytes = _state.ImageCache.GetImage(image.Path); if (bytes != null) { var ms = new System.IO.MemoryStream(bytes); var bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); bi.Freeze(); if (_enableImageCache) _biCache[image.Path] = bi; _dc.DrawImage(bi, rect); } } catch (Exception ex) { Debug.Print(ex.Message); Debug.Print(ex.StackTrace); } } }
/// <summary> /// /// </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); }