/// <summary> /// Initializes a new instance of the <see cref="DrawingContextImpl"/> class. /// </summary> /// <param name="visualBrushRenderer">The visual brush renderer.</param> /// <param name="renderTarget">The render target to draw to.</param> /// <param name="layerFactory"> /// An object to use to create layers. May be null, in which case a /// <see cref="WicRenderTargetBitmapImpl"/> will created when a new layer is requested. /// </param> /// <param name="swapChain">An optional swap chain associated with this drawing context.</param> /// <param name="finishedCallback">An optional delegate to be called when context is disposed.</param> public DrawingContextImpl( IVisualBrushRenderer visualBrushRenderer, ILayerFactory layerFactory, SharpDX.Direct2D1.RenderTarget renderTarget, SharpDX.DXGI.SwapChain1 swapChain = null, Action finishedCallback = null) { _visualBrushRenderer = visualBrushRenderer; _layerFactory = layerFactory; _renderTarget = renderTarget; _swapChain = swapChain; _finishedCallback = finishedCallback; if (_renderTarget is DeviceContext deviceContext) { _deviceContext = deviceContext; _ownsDeviceContext = false; } else { _deviceContext = _renderTarget.QueryInterface <DeviceContext>(); _ownsDeviceContext = true; } _deviceContext.BeginDraw(); }
public void Render() { if (_bitmap1 == null || _renderTarget2D == null) { return; } DeviceContext.Target = _bitmap1; using (DeviceContext.Target) { DeviceContext.BeginDraw(); if (this.needsClear) { DeviceContext.Clear(this.ClearColor); this.needsClear = false; } while (_queue.Count > 0) { CanvasObject o = _queue.Dequeue(); o.DoWork(DeviceContext); } DeviceContext.EndDraw(); } DeviceContext.Target = null; _spriteBatch.Begin(SpriteSortMode.Immediate, this._graphicsDeviceManager.GraphicsDevice.BlendStates.NonPremultiplied); _spriteBatch.Draw(_renderTarget2D, Vector2.Zero, Color.White); _spriteBatch.End(); }
// 進行と描画 public void 進行描画する(DeviceContext d2ddc, ランク種別 rank) { if (this._初めての進行描画) { this._初めての進行描画 = false; this._Rank = rank; // 0.6 秒後にエフェクト開始 this._エフェクト開始タイマ = new Timer((state) => { this._エフェクト開始イベント.Set(); }, null, 600, Timeout.Infinite); } if (0 > this._ランクエフェクトハンドル) { if (this._エフェクト開始イベント.IsSet) { this._ランクエフェクトを開始する(); this._エフェクト開始イベント.Reset(); } } else { d2ddc.EndDraw(); // D2D中断 Global.Effekseer.描画する(this._ランクエフェクトハンドル); d2ddc.BeginDraw(); // D2D再開 } }
private void Render() { DeviceContext renderTarget = _deviceContext; renderTarget.BeginDraw(); List <Tuple <Geometry, Color> > copy = _task == null ? new List <Tuple <Geometry, Color> >() : _task.Result; _task = CreateGeometries(_time); if (_time == 0.1f) { renderTarget.Clear(Color.FromKnown(Colors.Black, 1f)); } renderTarget.FillRect(_brush, new RectF(0, 0, ClientSize.Width, ClientSize.Height)); for (int index = 0; index < copy.Count; ++index) { Tuple <Geometry, Color> tuple = copy[index]; using (Geometry geometry = tuple.Item1) { using (SolidColorBrush brush = renderTarget.CreateSolidColorBrush(tuple.Item2.AdjustContrast(1.5f))) { renderTarget.DrawGeometry(brush, 0.1f, geometry); } } } renderTarget.EndDraw(); _swapChain.Present(1, 0); _time += 0.002f; }
public void PopRenderTarget() { DeviceContext.EndDraw(); DeviceContext.Target = null; HasTarget = false; targetStack.Pop(); if (targetStack.Count > 0) { DeviceContext.Target = targetStack.Peek(); HasTarget = true; DeviceContext.BeginDraw(); } }
public void Display(bool clear = false) { Device.Target = Device.BackBuffer; DeviceContext context = Device; context.BeginDraw(); if (clear) { context.Clear(Color.Transparent); } Render(); context.EndDraw(); }
public void PushRenderTarget(BitmapProxy target, bool clear) { if (targetStack.Count > 0) { DeviceContext.EndDraw(); } targetStack.Push(target); DeviceContext.Target = targetStack.Peek(); HasTarget = true; DeviceContext.BeginDraw(); if (clear) { DeviceContext.Clear(Color.Transparent); } }
protected override void OnDxPaint(Dx11Component resource) { DeviceContext context = resource.D2D.Context; Size clientSize = GetClientSize(); if (frames % framesPerTick == 0) { for (int i = 0; i < ticksPerFrame; i++) { update |= circuitBoard.Update(); performanceMonitor.IncrementTicks(); } if (update) { circuitBoard.Image.CopyToBitmap(bitmap); update = false; } frames = 0; } frames++; context.BeginDraw(); context.Clear(clearColor); context.Transform = transformationMatrix; context.DrawBitmap(bitmap, CreateImageRect(bitmap.PixelSize, clientSize), 1f, BitmapInterpolationMode.NearestNeighbor); context.Transform = identityMatrix; if (performanceMonitor.TryCalculate()) { performaceText = $"{performanceMonitor.FramesPerSecond:F0} FPS | {performanceMonitor.TicksPerSecond:F0} TPS"; } context.DrawText(performaceText, textFormat, new RawRectangleF(0f, 0f, clientSize.Width, clientSize.Height), textBrush); context.EndDraw(); performanceMonitor.IncermentFrames(); }
// 進行と描画 public void 進行描画する(DeviceContext dc, ランク種別 rank) { this._Rank = rank; if (0 > this._ランクエフェクトハンドル && this._エフェクト開始イベント.IsSet) { this._ランクエフェクトを開始する(); this._エフェクト開始イベント.Reset(); } if (0 <= this._ランクエフェクトハンドル) { dc.EndDraw(); // D2D中断 Global.Effekseer.描画する(this._ランクエフェクトハンドル); dc.BeginDraw(); // D2D再開 } }
public void Draw(Window window) { FrameResource frameResource = FrameResources[Presenter.BackBufferIndex]; Device3D.AcquireWrappedResources(new[] { frameResource.WrappedBackBuffer }, 1); var rectangle = new RectangleF(0, 0, 200, 200); DeviceContext.Target = frameResource.Bitmap; //DeviceContext.AntialiasMode = AntialiasMode.Aliased; DeviceContext.BeginDraw(); DeviceContext.Transform = Matrix3x2.Identity; window?.DrawChildren(DrawContext); var b = new RectangleF(50f, 50f, 100, 100); //DeviceContext.PushAxisAlignedClip(b, AntialiasMode.PerPrimitive); // https://github.com/Microsoft/DirectX-Graphics-Samples/issues/212 //DeviceContext.FillRectangle(b, Brush); //DeviceContext.Clear(new Color4(1, 1, 1, 1)); //DeviceContext.PopAxisAlignedClip(); //DeviceContext.Clear(new Color4(1, 1, 1, 1)); //DeviceContext.Flush(); DeviceContext.EndDraw(); Device3D.ReleaseWrappedResources(new[] { frameResource.WrappedBackBuffer }, 1); DeviceContext3D.Flush(); /* * PathGeometry geometry = new PathGeometry(Factory); * GeometrySink sink = geometry.Open(); * sink.Close(); */ }
private void DrawCharacters(Bitmap1 target, string characters, ImmutableDictionary <int, string> puaCharacters, bool drawOutline, int offsetX, int offsetY, string fontFamily, int fontSize, int baselineOriginX, int baselineOriginY) { _drawingOutline = drawOutline; _puaCharacters = puaCharacters; _baselineOrigin = new Vector2(baselineOriginX, baselineOriginY); _widths = new byte[characters.Length]; _idxCurrent = 0; DeviceContext.Target = target; DeviceContext.Transform = Matrix3x2.Identity; CreateBrushes(); using (_textFormat = new TextFormat(DWriteFactory, fontFamily, FontWeight.Regular, FontStyle.Normal, FontStretch.Normal, fontSize)) using (_outlineRenderer = new OutlineRenderer(DeviceContext)) { _textFormat.WordWrapping = WordWrapping.NoWrap; DeviceContext.BeginDraw(); #if DEBUG DrawGridLines(); #endif DeviceContext.Transform = Matrix3x2.Translation(offsetX, offsetY); for (int i = 0; i < characters.Length; i += ColumnCount) { int currentRowLength = Math.Min(ColumnCount, characters.Length - i); string currentRow = characters.Substring(i, currentRowLength); DrawRow(currentRow); var transform = Matrix3x2.Multiply(DeviceContext.Transform, Matrix3x2.Translation(0, _cellHeight)); DeviceContext.Transform = transform; } DeviceContext.EndDraw(); } DestroyBrushes(); DeviceContext.Target = null; }
private async Task Render() { DeviceContext renderTarget = _deviceContext; renderTarget.BeginDraw(); renderTarget.Clear(Color.FromKnown(Colors.Black, 1f)); for (int index = 0; index < PointCount; ++index) { for (int n = 0; n < NeighborCount; ++n) { renderTarget.DrawLine(_brush1, (NeighborCount - n) / (float)(10 * NeighborCount), _points[index], _points[_neighbors[index, n]]); } renderTarget.DrawEllipse(_brush, 0.5f, new Ellipse(_points[index], 1.5f, 1.5f)); } string text = "Press <Esc> to exit..."; using (TextFormat textFormat = _directWriteFactory.CreateTextFormat("Segoe UI", 13, FontWeight.Normal)) using (TextLayout textLayout = _directWriteFactory.CreateTextLayout(text, textFormat, float.MaxValue, float.MaxValue)) using (SolidColorBrush textBrush = renderTarget.CreateSolidColorBrush(Color.FromKnown(Colors.White, 1))) { renderTarget.DrawText( text, textFormat, new RectF(10, 10, ClientRectangle.Width, ClientRectangle.Height), textBrush, DrawTextOptions.None, MeasuringMode.Natural ); } renderTarget.EndDraw(); _swapChain.Present(1, 0); await MovePoints(); }
private void Render() { DeviceContext renderTarget = _deviceContext; renderTarget.BeginDraw(); renderTarget.Clear(Color.FromKnown(Colors.Black, 1f)); _random = new Random(19292); for (int index = 0; index < _brushes.Length; ++index) { float x = _random.Next(0, ClientRectangle.Width); float y = _random.Next(0, ClientRectangle.Height); Ellipse ellipse = new Ellipse(x, y, 50, 50); renderTarget.FillEllipse(_brushes[index], ellipse); renderTarget.DrawEllipse(_pens[index], 1, ellipse); } string text = "Press <Esc> to exit..."; using (TextFormat textFormat = _directWriteFactory.CreateTextFormat("Segoe UI", 13, FontWeight.Normal)) using (TextLayout textLayout = _directWriteFactory.CreateTextLayout(text, textFormat, float.MaxValue, float.MaxValue)) using (SolidColorBrush textBrush = renderTarget.CreateSolidColorBrush(Color.FromKnown(Colors.White, 1))) { renderTarget.DrawText( text, textFormat, new RectF(10, 10, ClientRectangle.Width, ClientRectangle.Height), textBrush, DrawTextOptions.None, MeasuringMode.Natural ); } renderTarget.EndDraw(); _swapChain.Present(1, 0); }
public BitmapResource ApplyTo(BitmapResource pBitmap, Size pScale) { var b = new Bitmap1(_context, new SharpDX.Size2((int)pScale.Width, (int)pScale.Height), new BitmapProperties1() { PixelFormat = pBitmap.DirectXBitmap.PixelFormat, BitmapOptions = BitmapOptions.Target }); var t = _context.Target; _context.Target = b; _context.BeginDraw(); DrawInternal(_context, pBitmap, Vector2.Zero, pScale); _context.EndDraw(); _context.Target = t; return(new BitmapResource() { DirectXBitmap = b }); }
private void DoInsert(Bitmap1 target, string fontFamily, string characters, int originY) { DeviceContext.Target = target; using (var font = GetSystemFont(fontFamily)) { DeviceContext.BeginDraw(); #if DEBUG DrawGridLines(); #endif DeviceContext.Transform = Matrix3x2.Translation(0, originY); for (int i = 0; i < characters.Length; i += ColumnCount) { int currentRowLength = Math.Min(ColumnCount, characters.Length - i); string currentRow = characters.Substring(i, currentRowLength); using (var glyphRun = new GlyphRun()) { glyphRun.FontFace = new FontFace(font); glyphRun.FontSize = FontSize; glyphRun.Indices = glyphRun.FontFace.GetGlyphIndices(currentRow.Select(c => (int)c).ToArray()); glyphRun.Advances = Enumerable.Repeat <float>(CellWidth, currentRowLength).ToArray(); var baselineOrigin = new Vector2(1.0f, 41.0f); DeviceContext.PushAxisAlignedClip(new RectangleF(0, 0, CellWidth * ColumnCount, CellHeight), AntialiasMode.PerPrimitive); DeviceContext.DrawGlyphRun(baselineOrigin, glyphRun, WhiteBrush, MeasuringMode.Natural); DeviceContext.PopAxisAlignedClip(); } var transform = Matrix3x2.Multiply(DeviceContext.Transform, Matrix3x2.Translation(0, CellHeight)); DeviceContext.Transform = transform; } DeviceContext.EndDraw(); } DeviceContext.Target = null; }
private void Render() { DeviceContext renderTarget = _deviceContext; renderTarget.BeginDraw(); renderTarget.Clear(Color.FromKnown(Colors.Black, 1f)); for (double re = 0.5; re <= 2; re += 0.1d) { for (double im = -2; im <= 2; im += 0.1d) { Complex s = new Complex(re, im); Complex z = AdvancedComplexMath.RiemannZeta(s); PointF rs = TranslateToScreen(s.Real, s.Imaginary); PointF rz = TranslateToScreen(z.Real, z.Imaginary); renderTarget.DrawLine(_brush1, 1, rs, rz); } } renderTarget.EndDraw(); _swapChain.Present(1, 0); }
internal void Start() { Context2D.BeginDraw(); }
public void TestDraw() { d2d1DC.BeginDraw(); d2d1DC.Clear(Color.Black); //============================= SolidColorBrush brush = new SolidColorBrush(d2d1DC, Color.Yellow); SolidColorBrush brush2 = new SolidColorBrush(d2d1DC, Color.YellowGreen); TextFormat textFormat = new TextFormat(DirectXFactory.DWFactory, PreferFont, FontSize); TextFormat textFormat2 = new TextFormat(DirectXFactory.DWFactory, PreferFont, FontSize); String msg = "正"; TextLayout textLayout = new TextLayout(DirectXFactory.DWFactory, msg, textFormat, textFormat.FontSize * 1.5f, textFormat.FontSize); Size2F s = new Size2F(textLayout.Metrics.Width, textLayout.Metrics.Height); float d = s.Width / 2.0f; RectangleF rect = new RectangleF(0, 0, d, s.Height); textFormat.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading; textFormat2.TextAlignment = SharpDX.DirectWrite.TextAlignment.Trailing; textFormat.WordWrapping = WordWrapping.NoWrap; //遇到邊界時別下移 DrawTextOptions op = DrawTextOptions.Clip; //遇到邊界時裁切 float oy = (textLayout.Metrics.Height - textFormat.FontSize) / 2.0f; RectangleF rect1 = new RectangleF(0, oy, textLayout.Metrics.Width / 2.0f, textFormat.FontSize); RectangleF rect2 = new RectangleF(0, oy + textFormat.FontSize, textLayout.Metrics.Width / 2.0f, textFormat.FontSize); d2d1DC.FillRectangle(rect1, new SolidColorBrush(d2d1DC, Color.White)); d2d1DC.FillRectangle(rect2, new SolidColorBrush(d2d1DC, Color.Blue)); d2d1DC.DrawText("你", textFormat, rect, brush, op); rect.X += d; d2d1DC.DrawText("你", textFormat2, rect, brush2, op); rect.X += d; d2d1DC.DrawText("怎", textFormat, rect, brush, op); rect.X += d; d2d1DC.DrawText("怎", textFormat2, rect, brush2, op); rect.X += d; d2d1DC.DrawText("不", textFormat, rect, brush, op); rect.X += d; d2d1DC.DrawText("不", textFormat2, rect, brush2, op); rect.X += d; d2d1DC.DrawText("問", textFormat, rect, brush, op); rect.X += d; d2d1DC.DrawText("問", textFormat2, rect, brush2, op); rect.X += d; d2d1DC.DrawText("問", textFormat, rect, brush, op); rect.X += d; d2d1DC.DrawText("問", textFormat2, rect, brush2, op); rect.X += d; d2d1DC.DrawText("神", textFormat, rect, brush, op); rect.X += d; d2d1DC.DrawText("神", textFormat2, rect, brush2, op); rect.X += d; d2d1DC.DrawText("奇", textFormat, rect, brush, op); rect.X += d; d2d1DC.DrawText("奇", textFormat2, rect, brush2, op); rect.X += d; d2d1DC.DrawText("海", textFormat, rect, brush, op); rect.X += d; d2d1DC.DrawText("海", textFormat2, rect, brush2, op); rect.X += d; d2d1DC.DrawText("螺", textFormat, rect, brush, op); rect.X += d; d2d1DC.DrawText("螺", textFormat2, rect, brush2, op); //============================= d2d1DC.EndDraw(); swapChain.Present(1, PresentFlags.None); }
public System.Drawing.Bitmap TextToBitmap(string text, out Size2F size, RawColor4 bgcolor, d2.TextAntialiasMode aamode, int maxWidth = 1000, int maxHeight = 1000) { var sz = GetTextSize(text, maxWidth, maxHeight); int pixelWidth = (int)(sz.Width * 2); int pixelHeight = (int)(sz.Height * 2); var d2dRenderTarget = new Bitmap1(d2dContext, new Size2(pixelWidth, pixelHeight), d2dBitmapProps); if (d2dContext.Target != null) { d2dContext.Target.Dispose(); } d2dContext.Target = d2dRenderTarget; // associate bitmap with the d2d context // Draw Text TextLayout textLayout = new TextLayout(dwFactory, text, textFormat, pixelWidth, pixelHeight); //d2dContext.TextRenderingParams = new RenderingParams(dwFactory, 1, 0, 0, PixelGeometry.Flat, renderingMode); d2dContext.TextAntialiasMode = aamode; d2dContext.BeginDraw(); d2dContext.Clear(bgcolor); d2dContext.DrawTextLayout(new RawVector2(0, 0), textLayout, textBrush, DrawTextOptions.EnableColorFont); d2dContext.EndDraw(); size = new Size2F(textLayout.Metrics.Width, textLayout.Metrics.Height); textLayout.Dispose(); // Copy to MemoryStream var stream = new MemoryStream(); var encoder = new wic.PngBitmapEncoder(imagingFactory); encoder.Initialize(stream); var bitmapFrameEncode = new wic.BitmapFrameEncode(encoder); bitmapFrameEncode.Initialize(); bitmapFrameEncode.SetSize(pixelWidth, pixelHeight); bitmapFrameEncode.SetPixelFormat(ref wicPixelFormat); // this is the trick to write D2D1 bitmap to WIC var imageEncoder = new wic.ImageEncoder(imagingFactory, d2dDevice); var imageParam = new wic.ImageParameters(d2PixelFormat, dpi, dpi, 0, 0, pixelWidth, pixelHeight); imageEncoder.WriteFrame(d2dRenderTarget, bitmapFrameEncode, imageParam); bitmapFrameEncode.Commit(); encoder.Commit(); imageEncoder.Dispose(); encoder.Dispose(); bitmapFrameEncode.Dispose(); d2dRenderTarget.Dispose(); // Convert To Bitmap byte[] data = stream.ToArray(); stream.Seek(0, SeekOrigin.Begin); var bmp = new System.Drawing.Bitmap(stream); stream.Dispose(); return(bmp); }