internal static async Task Render(CompositionEngine compositionEngine, SharpDX.Direct2D1.RenderTarget renderTarget, FrameworkElement rootElement, Line line) { var rect = line.GetBoundingRect(rootElement).ToSharpDX(); //var fill = line.Fill.ToSharpDX(renderTarget, rect); var stroke = await line.Stroke.ToSharpDX(renderTarget, rect); if (stroke == null || line.StrokeThickness <= 0) { return; } //var layer = new Layer(renderTarget); //var layerParameters = new LayerParameters(); //layerParameters.ContentBounds = rect; //renderTarget.PushLayer(ref layerParameters, layer); renderTarget.DrawLine( new DrawingPointF( rect.Left + (float)line.X1, rect.Top + (float)line.Y1), new DrawingPointF( rect.Left + (float)line.X2, rect.Top + (float)line.Y2), stroke, (float)line.StrokeThickness, line.GetStrokeStyle(compositionEngine.D2DFactory)); //renderTarget.PopLayer(); }
internal static async Task Render(CompositionEngine compositionEngine, SharpDX.Direct2D1.RenderTarget renderTarget, FrameworkElement rootElement, Line line) { var rect = line.GetBoundingRect(rootElement).ToSharpDX(); var stroke = await line.Stroke.ToSharpDX(renderTarget, rect); if (stroke == null || line.StrokeThickness <= 0) { return; } var layer = line.CreateAndPushLayerIfNecessary(renderTarget, rootElement); renderTarget.DrawLine( new Vector2( rect.Left + (float)line.X1, rect.Top + (float)line.Y1), new Vector2( rect.Left + (float)line.X2, rect.Top + (float)line.Y2), stroke, (float)line.StrokeThickness, line.GetStrokeStyle(compositionEngine.D2DFactory)); if (layer != null) { renderTarget.PopLayer(); layer.Dispose(); } }
public void DrawLine(float x0, float y0, float x1, float y1, System.Drawing.Color clr) { SharpDX.Mathematics.Interop.RawVector2 p0 = new SharpDX.Mathematics.Interop.RawVector2 { X = x0, Y = y0 }; SharpDX.Mathematics.Interop.RawVector2 p1 = new SharpDX.Mathematics.Interop.RawVector2 { X = x1, Y = y1 }; SharpDX.Direct2D1.SolidColorBrush brush = new SharpDX.Direct2D1.SolidColorBrush(d2dRenderTarget, ToColor(clr)); d2dRenderTarget.DrawLine(p0, p1, brush); brush.Dispose(); }
private void DirectXRender(List<UIElement> points) { if (points.Count <= 0) return; Line initLine = points[0] as Line; Point p1 = new Point ( Math.Min(initLine.X1 - initLine.StrokeThickness / 2, initLine.X2 - initLine.StrokeThickness / 2), Math.Min(initLine.Y1 - initLine.StrokeThickness / 2, initLine.Y2 - initLine.StrokeThickness / 2) ), p2 = new Point ( Math.Max(initLine.X1 + initLine.StrokeThickness / 2, initLine.X2 + initLine.StrokeThickness / 2), Math.Max(initLine.Y1 + initLine.StrokeThickness / 2, initLine.Y2 + initLine.StrokeThickness / 2) ); foreach (var child in points) { var line = child as Line; if (line == null) continue; if (p1.X > line.X1 - line.StrokeThickness / 2) p1.X = line.X1 - line.StrokeThickness / 2; if (p1.X > line.X2 - line.StrokeThickness / 2) p1.X = line.X2 - line.StrokeThickness / 2; if (p2.X < line.X1 + line.StrokeThickness / 2) p2.X = line.X1 + line.StrokeThickness / 2; if (p2.X < line.X2 + line.StrokeThickness / 2) p2.X = line.X2 + line.StrokeThickness / 2; if (p1.Y > line.Y1 - line.StrokeThickness / 2) p1.Y = line.Y1 - line.StrokeThickness / 2; if (p1.Y > line.Y2 - line.StrokeThickness / 2) p1.Y = line.Y2 - line.StrokeThickness / 2; if (p2.Y < line.Y1 + line.StrokeThickness / 2) p2.Y = line.Y1 + line.StrokeThickness / 2; if (p2.Y < line.Y2 + line.StrokeThickness / 2) p2.Y = line.Y2 + line.StrokeThickness / 2; } var bndRect = new Rect(p1, p2); var dxTarget = new SurfaceImageSource ( (int)(bndRect.Width * DisplayProperties.LogicalDpi / 96.0 + 1), (int)(bndRect.Height * DisplayProperties.LogicalDpi / 96.0 + 1) ); SharpDX.DXGI.ISurfaceImageSourceNative dxTargetNative = SharpDX.ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(dxTarget); dxTargetNative.Device = d3dDevice.QueryInterface<SharpDX.DXGI.Device>(); /* * Draw Logic */ SharpDX.DrawingPoint drawingPoint; var surface = dxTargetNative.BeginDraw(new SharpDX.Rectangle(0, 0, (int)(bndRect.Width * DisplayProperties.LogicalDpi / 96.0 + 1), (int)(bndRect.Height * DisplayProperties.LogicalDpi / 96.0 + 1)), out drawingPoint); var dxRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, surface, new SharpDX.Direct2D1.RenderTargetProperties() { DpiX = DisplayProperties.LogicalDpi, DpiY = DisplayProperties.LogicalDpi, PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), Type = SharpDX.Direct2D1.RenderTargetType.Default, Usage = SharpDX.Direct2D1.RenderTargetUsage.None }); dxRenderTarget.BeginDraw(); dxRenderTarget.Clear(SharpDX.Color.Transparent); foreach (var child in points) { var line = child as Line; if (line == null) continue; Color c = (line.Stroke as SolidColorBrush).Color; var brush = new SharpDX.Direct2D1.SolidColorBrush(dxRenderTarget, new SharpDX.Color(c.R, c.G, c.B, c.A)); var style = new SharpDX.Direct2D1.StrokeStyleProperties(); style.LineJoin = SharpDX.Direct2D1.LineJoin.Round; style.StartCap = SharpDX.Direct2D1.CapStyle.Round; style.EndCap = SharpDX.Direct2D1.CapStyle.Round; var stroke = new SharpDX.Direct2D1.StrokeStyle(d2dFactory, style); dxRenderTarget.DrawLine( new SharpDX.DrawingPointF((float)(line.X1 - bndRect.Left), (float)(line.Y1 - bndRect.Top)), new SharpDX.DrawingPointF((float)(line.X2 - bndRect.Left), (float)(line.Y2 - bndRect.Top)), brush, (float)line.StrokeThickness, stroke); } dxRenderTarget.EndDraw(); dxTargetNative.EndDraw(); var dxImage = new Image(); dxImage.Source = dxTarget; canvas.Children.Add(dxImage); Canvas.SetLeft(dxImage, bndRect.X); Canvas.SetTop(dxImage, bndRect.Y); }
private void DirectXRender(List <UIElement> points) { if (points.Count <= 0) { return; } Line initLine = points[0] as Line; Point p1 = new Point ( Math.Min(initLine.X1 - initLine.StrokeThickness / 2, initLine.X2 - initLine.StrokeThickness / 2), Math.Min(initLine.Y1 - initLine.StrokeThickness / 2, initLine.Y2 - initLine.StrokeThickness / 2) ), p2 = new Point ( Math.Max(initLine.X1 + initLine.StrokeThickness / 2, initLine.X2 + initLine.StrokeThickness / 2), Math.Max(initLine.Y1 + initLine.StrokeThickness / 2, initLine.Y2 + initLine.StrokeThickness / 2) ); foreach (var child in points) { var line = child as Line; if (line == null) { continue; } if (p1.X > line.X1 - line.StrokeThickness / 2) { p1.X = line.X1 - line.StrokeThickness / 2; } if (p1.X > line.X2 - line.StrokeThickness / 2) { p1.X = line.X2 - line.StrokeThickness / 2; } if (p2.X < line.X1 + line.StrokeThickness / 2) { p2.X = line.X1 + line.StrokeThickness / 2; } if (p2.X < line.X2 + line.StrokeThickness / 2) { p2.X = line.X2 + line.StrokeThickness / 2; } if (p1.Y > line.Y1 - line.StrokeThickness / 2) { p1.Y = line.Y1 - line.StrokeThickness / 2; } if (p1.Y > line.Y2 - line.StrokeThickness / 2) { p1.Y = line.Y2 - line.StrokeThickness / 2; } if (p2.Y < line.Y1 + line.StrokeThickness / 2) { p2.Y = line.Y1 + line.StrokeThickness / 2; } if (p2.Y < line.Y2 + line.StrokeThickness / 2) { p2.Y = line.Y2 + line.StrokeThickness / 2; } } var bndRect = new Rect(p1, p2); var dxTarget = new SurfaceImageSource ( (int)(bndRect.Width * DisplayProperties.LogicalDpi / 96.0 + 1), (int)(bndRect.Height * DisplayProperties.LogicalDpi / 96.0 + 1) ); SharpDX.DXGI.ISurfaceImageSourceNative dxTargetNative = SharpDX.ComObject.As <SharpDX.DXGI.ISurfaceImageSourceNative>(dxTarget); dxTargetNative.Device = d3dDevice.QueryInterface <SharpDX.DXGI.Device>(); /* * Draw Logic */ SharpDX.DrawingPoint drawingPoint; var surface = dxTargetNative.BeginDraw(new SharpDX.Rectangle(0, 0, (int)(bndRect.Width * DisplayProperties.LogicalDpi / 96.0 + 1), (int)(bndRect.Height * DisplayProperties.LogicalDpi / 96.0 + 1)), out drawingPoint); var dxRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, surface, new SharpDX.Direct2D1.RenderTargetProperties() { DpiX = DisplayProperties.LogicalDpi, DpiY = DisplayProperties.LogicalDpi, PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), Type = SharpDX.Direct2D1.RenderTargetType.Default, Usage = SharpDX.Direct2D1.RenderTargetUsage.None }); dxRenderTarget.BeginDraw(); dxRenderTarget.Clear(SharpDX.Color.Transparent); foreach (var child in points) { var line = child as Line; if (line == null) { continue; } Color c = (line.Stroke as SolidColorBrush).Color; var brush = new SharpDX.Direct2D1.SolidColorBrush(dxRenderTarget, new SharpDX.Color(c.R, c.G, c.B, c.A)); var style = new SharpDX.Direct2D1.StrokeStyleProperties(); style.LineJoin = SharpDX.Direct2D1.LineJoin.Round; style.StartCap = SharpDX.Direct2D1.CapStyle.Round; style.EndCap = SharpDX.Direct2D1.CapStyle.Round; var stroke = new SharpDX.Direct2D1.StrokeStyle(d2dFactory, style); dxRenderTarget.DrawLine( new SharpDX.DrawingPointF((float)(line.X1 - bndRect.Left), (float)(line.Y1 - bndRect.Top)), new SharpDX.DrawingPointF((float)(line.X2 - bndRect.Left), (float)(line.Y2 - bndRect.Top)), brush, (float)line.StrokeThickness, stroke); } dxRenderTarget.EndDraw(); dxTargetNative.EndDraw(); var dxImage = new Image(); dxImage.Source = dxTarget; canvas.Children.Add(dxImage); Canvas.SetLeft(dxImage, bndRect.X); Canvas.SetTop(dxImage, bndRect.Y); }