public override void OnRender(ChartControl chartControl, ChartScale chartScale, ChartBars chartBars) { Bars bars = chartBars.Bars; if (chartBars.FromIndex > 0) { chartBars.FromIndex--; } SharpDX.Direct2D1.PathGeometry lineGeometry = new SharpDX.Direct2D1.PathGeometry(Core.Globals.D2DFactory); GeometrySink sink = lineGeometry.Open(); sink.BeginFigure(new Vector2(chartControl.GetXByBarIndex(chartBars, chartBars.FromIndex > -1 ? chartBars.FromIndex : 0), chartScale.GetYByValue(bars.GetClose(chartBars.FromIndex > -1 ? chartBars.FromIndex : 0))), FigureBegin.Filled); for (int idx = chartBars.FromIndex + 1; idx <= chartBars.ToIndex; idx++) { double closeValue = bars.GetClose(idx); float close = chartScale.GetYByValue(closeValue); float x = chartControl.GetXByBarIndex(chartBars, idx); sink.AddLine(new Vector2(x, close)); } sink.EndFigure(FigureEnd.Open); sink.Close(); AntialiasMode oldAliasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = AntialiasMode.PerPrimitive; RenderTarget.DrawGeometry(lineGeometry, UpBrushDX, (float)Math.Max(1, chartBars.Properties.ChartStyle.BarWidth)); RenderTarget.AntialiasMode = oldAliasMode; lineGeometry.Dispose(); }
private void MainWindow_Load(object sender, EventArgs e) { DirectWriteFactory f = DirectWriteFactory.Create(DirectWriteFactoryType.Shared); _textFormat = f.CreateTextFormat("Verdana", 110, FontWeight.Normal, FontStyle.Normal, FontStretch.Normal); _textFormat.ParagraphAlignment = ParagraphAlignment.Far; _textFormat.TextAlignment = TextAlignment.Center; ClientSize = new System.Drawing.Size(600, 600); _factory = Direct2DFactory.CreateFactory(FactoryType.SingleThreaded, DebugLevel.None, FactoryVersion.Auto); _renderTarget = _factory.CreateWindowRenderTarget(this, PresentOptions.None, RenderTargetProperties.Default); AntialiasMode amode = _renderTarget.AntialiasMode; TextAntialiasMode tamode = _renderTarget.TextAntialiasMode; _strokeBrush = _renderTarget.CreateSolidColorBrush(Color.FromKnown(Colors.Cyan, 1)); try { _strokeStyle = _factory.CreateStrokeStyle(new StrokeStyleProperties1(LineCapStyle.Flat, LineCapStyle.Flat, LineCapStyle.Round, LineJoin.Miter, 10, DashStyle.Dot, 0, StrokeTransformType.Normal), null); } catch (NotSupportedException) { _strokeStyle = _factory.CreateStrokeStyle(new StrokeStyleProperties(LineCapStyle.Flat, LineCapStyle.Flat, LineCapStyle.Round, LineJoin.Miter, 10, DashStyle.Dot, 0), null); } Resize += new EventHandler(MainWindow_Resize); }
// opt private void SetAntialiasMode(bool aliased) { var antialiasMode = aliased ? AntialiasMode.Aliased : AntialiasMode.PerPrimitive; if (_antialiasMode != antialiasMode) { _antialiasMode = antialiasMode; _renderTarget.AntialiasMode = antialiasMode; } }
/// <summary> /// Initializes a new instance of the <see cref="LayerParameters1"/> struct. /// </summary> /// <param name="contentBounds">The content bounds.</param> /// <param name="geometryMask">The geometry mask.</param> /// <param name="maskAntialiasMode">The mask antialias mode.</param> /// <param name="maskTransform">The mask transform.</param> /// <param name="opacity">The opacity.</param> /// <param name="opacityBrush">The opacity brush.</param> /// <param name="layerOptions">The layer options.</param> public LayerParameters1(RawRectangleF contentBounds, Geometry geometryMask, AntialiasMode maskAntialiasMode, RawMatrix3x2 maskTransform, float opacity, Brush opacityBrush, LayerOptions1 layerOptions) : this() { ContentBounds = contentBounds; geometricMask_ = geometryMask.NativePointer; MaskAntialiasMode = maskAntialiasMode; MaskTransform = maskTransform; Opacity = opacity; opacityBrush_ = opacityBrush.NativePointer; LayerOptions = layerOptions; }
/// <summary> /// Initializes a new instance of the <see cref="LayerParameters1"/> struct. /// </summary> /// <param name="contentBounds">The content bounds.</param> /// <param name="geometryMask">The geometry mask.</param> /// <param name="maskAntialiasMode">The mask antialias mode.</param> /// <param name="maskTransform">The mask transform.</param> /// <param name="opacity">The opacity.</param> /// <param name="opacityBrush">The opacity brush.</param> /// <param name="layerOptions">The layer options.</param> public LayerParameters1(RawRectangleF contentBounds, Geometry geometryMask, AntialiasMode maskAntialiasMode, RawMatrix3x2 maskTransform, float opacity, Brush opacityBrush, LayerOptions1 layerOptions) : this() { ContentBounds = contentBounds; GeometricMask = geometryMask; MaskAntialiasMode = maskAntialiasMode; MaskTransform = maskTransform; Opacity = opacity; OpacityBrush = opacityBrush; LayerOptions = layerOptions; }
/// <summary> /// Initializes a new instance of the <see cref="LayerParameters1"/> struct. /// </summary> /// <param name="contentBounds">The content bounds.</param> /// <param name="geometryMask">The geometry mask.</param> /// <param name="maskAntialiasMode">The mask antialias mode.</param> /// <param name="maskTransform">The mask transform.</param> /// <param name="opacity">The opacity.</param> /// <param name="opacityBrush">The opacity brush.</param> /// <param name="layerOptions">The layer options.</param> public LayerParameters1(RectangleF contentBounds, Geometry geometryMask, AntialiasMode maskAntialiasMode, Matrix3x2 maskTransform, float opacity, Brush opacityBrush, LayerOptions1 layerOptions) : this() { ContentBounds = contentBounds; geometricMask_ = geometryMask.NativePointer; MaskAntialiasMode = maskAntialiasMode; MaskTransform = maskTransform; Opacity = opacity; opacityBrush_ = opacityBrush.NativePointer; LayerOptions = layerOptions; }
/// <unmanaged>HRESULT ID2D1CommandSink::PushAxisAlignedClip([In] const D2D_RECT_F* clipRect,[In] D2D1_ANTIALIAS_MODE antialiasMode)</unmanaged> public void PushAxisAlignedClip(RawRectangleF clipRect, AntialiasMode antialiasMode) { PushAxisAlignedClip_(clipRect, antialiasMode); }
public override void OnRender(ChartControl chartControl, ChartScale chartScale, ChartBars chartBars) { double boxHeightActual = Math.Floor(10000000.0 * chartBars.Bars.BarsPeriod.Value * chartBars.Bars.Instrument.MasterInstrument.TickSize) / 10000000.0; int boxSize = (int)Math.Round(chartScale.Height.ConvertToVerticalPixels(chartControl.PresentationSource) / Math.Round(chartScale.MaxMinusMin / boxHeightActual, 0)); AntialiasMode oldAliasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = AntialiasMode.PerPrimitive; trendDetermined = false; for (int idx = chartBars.FromIndex; idx <= chartBars.ToIndex; idx++) { int barWidth = GetBarPaintWidth(BarWidthUI); double closeVal = chartBars.Bars.GetClose(idx); double openVal = chartBars.Bars.GetOpen(idx); int boxDrawCount = openVal == closeVal ? 1 : (int)Math.Round(Math.Abs(openVal - closeVal) / boxHeightActual, 0) + 1; float close = chartScale.GetYByValue(closeVal); float open = chartScale.GetYByValue(openVal); float nextBox = Math.Min(open, close); float x = chartControl.GetXByBarIndex(chartBars, idx); float diff = Math.Abs(open - close) + boxSize - (int)Math.Round((double)(boxSize * boxDrawCount)); if (closeVal == openVal) { if (idx == 0) { RenderTarget.DrawRectangle(new RectangleF(x - barWidth / 2.0f + 1, nextBox - boxSize / 2.0f + 2, barWidth - 1, boxSize - 2), DownBrushDX, Stroke.Width); Vector2 point0; Vector2 point1; point0.X = x - barWidth / 2.0f; point0.Y = nextBox - boxSize / 2.0f; point1.X = x + barWidth / 2.0f; point1.Y = nextBox + boxSize - boxSize / 2.0f; if (!(UpBrushDX is SharpDX.Direct2D1.SolidColorBrush)) { TransformBrush(UpBrushDX, new RectangleF(point0.X, point0.Y - Stroke.Width, barWidth, Stroke.Width)); } RenderTarget.DrawLine(point0, point1, UpBrushDX, Stroke.Width, Stroke.StrokeStyle); point0.X = x - barWidth / 2.0f; point0.Y = nextBox + boxSize - boxSize / 2.0f; point1.X = x + barWidth / 2.0f; point1.Y = nextBox - boxSize / 2.0f; if (!(UpBrushDX is SharpDX.Direct2D1.SolidColorBrush)) { TransformBrush(UpBrushDX, new RectangleF(point0.X, point0.Y - Stroke.Width, barWidth, Stroke.Width)); } RenderTarget.DrawLine(point0, point1, UpBrushDX, Stroke.Width, Stroke.StrokeStyle); continue; } if (!trendDetermined) { if (chartBars.Bars.GetOpen(idx - 1) == chartBars.Bars.GetClose(idx - 1)) { if (chartBars.Bars.GetHigh(idx) < chartBars.Bars.GetHigh(idx - 1)) { isUp = false; } } else { isUp = !(chartBars.Bars.GetOpen(idx - 1) < chartBars.Bars.GetClose(idx - 1)); } trendDetermined = true; } else { isUp = !isUp; } } else { isUp = closeVal > openVal; } for (int k = 0; k < boxDrawCount; k++) { if (diff != 0) { nextBox += diff > 0 ? 1 : -1; diff += diff > 0 ? -1 : 1; } if (!isUp) { Ellipse ellipse; ellipse.Point = new Vector2(x, nextBox); ellipse.RadiusX = barWidth / 2.0f; ellipse.RadiusY = boxSize / 2.0f - 1; if (!(DownBrushDX is SharpDX.Direct2D1.SolidColorBrush)) { TransformBrush(DownBrushDX, new RectangleF(ellipse.Point.X - ellipse.RadiusX, ellipse.Point.Y - ellipse.RadiusY - Stroke.Width, barWidth, Stroke.Width)); } RenderTarget.DrawEllipse(ellipse, DownBrushDX, Stroke.Width); } else { Vector2 point0; Vector2 point1; point0.X = x - barWidth / 2.0f; point0.Y = nextBox - boxSize / 2.0f; point1.X = x + barWidth / 2.0f; point1.Y = nextBox + boxSize - boxSize / 2.0f; if (!(UpBrushDX is SharpDX.Direct2D1.SolidColorBrush)) { TransformBrush(UpBrushDX, new RectangleF(point0.X, point0.Y - Stroke.Width, barWidth, Stroke.Width)); } RenderTarget.DrawLine(point0, point1, UpBrushDX, Stroke.Width, Stroke.StrokeStyle); point0.X = x - barWidth / 2.0f; point0.Y = nextBox + boxSize - boxSize / 2.0f; point1.X = x + barWidth / 2.0f; point1.Y = nextBox - boxSize / 2.0f; if (!(UpBrushDX is SharpDX.Direct2D1.SolidColorBrush)) { TransformBrush(UpBrushDX, new RectangleF(point0.X, point0.Y - Stroke.Width, barWidth, Stroke.Width)); } RenderTarget.DrawLine(point0, point1, UpBrushDX, Stroke.Width, Stroke.StrokeStyle); } nextBox += boxSize; } } RenderTarget.AntialiasMode = oldAliasMode; }
public override void OnRender(ChartControl chartControl, ChartScale chartScale, ChartBars chartBars) { Bars bars = chartBars.Bars; if (chartBars.FromIndex > 0) { chartBars.FromIndex--; } SharpDX.Direct2D1.PathGeometry lineGeometry = new SharpDX.Direct2D1.PathGeometry(Core.Globals.D2DFactory); AntialiasMode oldAliasMode = RenderTarget.AntialiasMode; GeometrySink sink = lineGeometry.Open(); sink.BeginFigure(new Vector2(chartControl.GetXByBarIndex(chartBars, chartBars.FromIndex > -1 ? chartBars.FromIndex : 0), chartScale.GetYByValue(bars.GetClose(chartBars.FromIndex > -1 ? chartBars.FromIndex : 0))), FigureBegin.Filled); for (int idx = chartBars.FromIndex + 1; idx <= chartBars.ToIndex; idx++) { double closeValue = bars.GetClose(idx); float close = chartScale.GetYByValue(closeValue); float x = chartControl.GetXByBarIndex(chartBars, idx); sink.AddLine(new Vector2(x, close)); } sink.EndFigure(FigureEnd.Open); sink.Close(); RenderTarget.AntialiasMode = AntialiasMode.PerPrimitive; RenderTarget.DrawGeometry(lineGeometry, UpBrushDX, (float)Math.Max(1, chartBars.Properties.ChartStyle.BarWidth)); lineGeometry.Dispose(); SharpDX.Direct2D1.SolidColorBrush fillOutline = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.Transparent); SharpDX.Direct2D1.PathGeometry fillGeometry = new SharpDX.Direct2D1.PathGeometry(Core.Globals.D2DFactory); GeometrySink fillSink = fillGeometry.Open(); fillSink.BeginFigure(new Vector2(chartControl.GetXByBarIndex(chartBars, chartBars.FromIndex > -1 ? chartBars.FromIndex : 0), chartScale.GetYByValue(chartScale.MinValue)), FigureBegin.Filled); float fillx = float.NaN; for (int idx = chartBars.FromIndex; idx <= chartBars.ToIndex; idx++) { double closeValue = bars.GetClose(idx); float close = chartScale.GetYByValue(closeValue); fillx = chartControl.GetXByBarIndex(chartBars, idx); fillSink.AddLine(new Vector2(fillx, close)); } if (!double.IsNaN(fillx)) { fillSink.AddLine(new Vector2(fillx, chartScale.GetYByValue(chartScale.MinValue))); } fillSink.EndFigure(FigureEnd.Open); fillSink.Close(); DownBrushDX.Opacity = Opacity / 100f; if (!(DownBrushDX is SharpDX.Direct2D1.SolidColorBrush)) { TransformBrush(DownBrushDX, new RectangleF(0, 0, (float)chartScale.Width, (float)chartScale.Height)); } RenderTarget.FillGeometry(fillGeometry, DownBrushDX); RenderTarget.DrawGeometry(fillGeometry, fillOutline, (float)chartBars.Properties.ChartStyle.BarWidth); fillOutline.Dispose(); RenderTarget.AntialiasMode = oldAliasMode; fillGeometry.Dispose(); }
public void BeginFrame() { _antialiasMode = _renderTarget.AntialiasMode; _renderTarget.BeginDraw(); }
private void RenderSelection(IDrawingContext dc, float scale, PointFloat offset, SelectionRenderParameters renderParams, Result <IGeometry> lazyGeometry, IBrush interiorBrush, IBrush outlineBrush, IStrokeStyle outlineStrokeStyle, AntialiasMode antialiasMode) { using (dc.UseScaleTransform(scale, scale, MatrixMultiplyOrder.Prepend)) { using (dc.UseTranslateTransform(offset.X, offset.Y, MatrixMultiplyOrder.Prepend)) { if (renderParams.IsInteriorFilled && (interiorBrush != null)) { using (dc.UseAntialiasMode(AntialiasMode.Aliased)) { dc.FillGeometry(lazyGeometry.Value, interiorBrush, null); } } if (renderParams.IsOutlineEnabled && (outlineBrush != null)) { float strokeWidth = this.outlineStrokeWidthPx / scale; using (dc.UseAntialiasMode(antialiasMode)) { using (dc.UseTranslateTransform(0.5f, 0.5f, MatrixMultiplyOrder.Append)) { dc.DrawGeometry(lazyGeometry.Value, outlineBrush, strokeWidth, outlineStrokeStyle); } } } } } }
private void RedrawOnBackgroundThread() { if (base.CheckAccess()) { ExceptionUtil.ThrowInvalidOperationException(); } if (this.redrawThreadFactory == null) { this.redrawThreadFactory = new Direct2DFactory(Direct2DFactoryType.MultiThreaded, DebugLevel.None); } if ((this.redrawThreadGeometryCache == null) || (this.redrawThreadGeometryCache.SelectionSnapshot != this.redrawRenderParams.SelectionSnapshot)) { DisposableUtil.Free <SelectionGeometryCache>(ref this.redrawThreadGeometryCache); this.redrawThreadGeometryCache = new SelectionGeometryCache(this.redrawThreadFactory, this.redrawRenderParams.SelectionSnapshot); } double scaleRatio = this.redrawRenderParams.ScaleRatio; PointDouble location = this.redrawRenderParams.ViewportCanvasBounds.Location; SizeDouble viewportSize = this.redrawRenderParams.ViewportSize; SizeInt32 num5 = SizeDouble.Ceiling(viewportSize); RectDouble b = RectDouble.Inflate(CanvasCoordinateConversions.ConvertExtentToViewport(CanvasCoordinateConversions.ConvertCanvasToExtent(this.redrawRenderParams.SelectionSnapshot.GeometryList.Value.Bounds, scaleRatio), scaleRatio, location), (double)this.outlineStrokeWidthPx, (double)this.outlineStrokeWidthPx); RectDouble a = new RectDouble(PointDouble.Zero, viewportSize); RectDouble renderViewportBounds = RectDouble.Intersect(a, b); RectInt32 viewportRect = renderViewportBounds.Int32Bound; RectDouble extentRect = CanvasCoordinateConversions.ConvertViewportToExtent(viewportRect, scaleRatio, location); RectDouble renderCanvasBounds = CanvasCoordinateConversions.ConvertExtentToCanvas(extentRect, scaleRatio); if (!viewportRect.HasPositiveArea) { base.SyncContext.Post((SendOrPostCallback)(_ => this.EndRedraw(renderViewportBounds, RectFloat.Zero, null, null))); } else { IBitmap <ColorAlpha8> interiorMask; float scale; PointFloat offset; Result <IGeometry> lazyGeometry; IBitmap <ColorAlpha8>[] dashedOutlineMasks; RectFloat maskSourceRect; SelectionSnapshot selectionSnapshot = this.redrawRenderParams.SelectionSnapshot; SelectionGeometryCache redrawThreadGeometryCache = this.redrawThreadGeometryCache; bool flag = this.redrawRenderParams.SelectionRenderingQuality == SelectionRenderingQuality.Aliased; if (!flag) { lazyGeometry = redrawThreadGeometryCache.Geometry; } else { bool flag3 = selectionSnapshot.IsRectilinear.Value; bool flag4 = selectionSnapshot.IsPixelated.Value; if (flag3 & flag4) { lazyGeometry = redrawThreadGeometryCache.Geometry; } else { lazyGeometry = redrawThreadGeometryCache.PixelatedGeometry; } } bool flag2 = !flag && this.redrawRenderParams.IsOutlineAntialiased; AntialiasMode antialiasMode = ((flag || !flag2) || selectionSnapshot.IsRectilinear.Value) ? AntialiasMode.Aliased : AntialiasMode.PerPrimitive; if (this.CanContinueRedrawing(null, null)) { scale = (float)this.redrawRenderParams.ScaleRatio; float x = -((float)renderCanvasBounds.X); float y = -((float)renderCanvasBounds.Y); offset = new PointFloat(x, y); interiorMask = BitmapAllocator.Alpha8.Allocate(viewportRect.Size, AllocationOptions.ZeroFillNotRequired); try { RetryManager.RunMemorySensitiveOperation(delegate { using (IDrawingContext context = DrawingContext.FromBitmap(this.redrawThreadFactory, interiorMask)) { context.Clear(null); IBrush interiorBrush = context.GetCachedOrCreateResource <IBrush>(whiteBrush); this.RenderSelection(context, scale, offset, this.redrawRenderParams, lazyGeometry, interiorBrush, null, null, antialiasMode); } }); } catch (OutOfMemoryException) { } if (this.CanContinueRedrawing(interiorMask, null)) { int num15; dashedOutlineMasks = new IBitmap <ColorAlpha8> [SelectionCanvasLayer.DashLength]; for (int i = 0; i < dashedOutlineMasks.Length; i = num15) { if ((!this.redrawRenderParams.IsOutlineAnimated && (i != 0)) && (i != (SelectionCanvasLayer.DashLength / 2))) { dashedOutlineMasks[i] = null; } else { dashedOutlineMasks[i] = BitmapAllocator.Alpha8.Allocate(viewportRect.Size, AllocationOptions.ZeroFillNotRequired); try { RetryManager.RunMemorySensitiveOperation(delegate { using (IDrawingContext context = DrawingContext.FromBitmap(this.redrawThreadFactory, dashedOutlineMasks[i])) { context.Clear(null); StrokeStyle resourceSource = SelectionCanvasLayer.GetDashedStrokeStyle(i); IStrokeStyle outlineStrokeStyle = context.GetCachedOrCreateResource <IStrokeStyle>(resourceSource); IBrush cachedOrCreateResource = context.GetCachedOrCreateResource <IBrush>(whiteBrush); this.RenderSelection(context, scale, offset, this.redrawRenderParams, lazyGeometry, null, cachedOrCreateResource, outlineStrokeStyle, antialiasMode); } }); } catch (OutOfMemoryException) { } if (!this.CanContinueRedrawing(interiorMask, dashedOutlineMasks)) { return; } } num15 = i + 1; } maskSourceRect = new RectFloat(PointFloat.Zero, interiorMask.Size); base.SyncContext.Post(_ => this.EndRedraw(renderCanvasBounds, maskSourceRect, interiorMask, dashedOutlineMasks), null); } } } }
public void SetTextAntialias(AntialiasMode antialiasMode) { deviceContext.AntialiasMode = antialiasMode; }