public void DrawRectangle( IElement element, IFrameContext context, PxRectangle bounds, PxPoint radius, Fill fill, Stroke strokeStyle ) { if (MathEx.IsZero(bounds.Width) || MathEx.IsZero(bounds.Height)) { return; } var fillBrush = fill == null ? null : CreateBrush(element, context, fill.Brush, fill.Opacity); var strokeBrush = strokeStyle.Width > 0 ? CreateBrush(element, context, strokeStyle.Brush, strokeStyle.Opacity) : null; var rect = bounds.ToDx(); if (radius.X > 0 || radius.Y > 0) { var roundedRect = new D2D1.RoundedRectangle() { Rect = rect, RadiusX = MathEx.IsZero(radius.X) ? radius.Y : radius.X, RadiusY = MathEx.IsZero(radius.Y) ? radius.X : radius.Y }; if (fillBrush != null) { Target.FillRoundedRectangle(roundedRect, fillBrush); } if (strokeBrush != null) { Target.DrawRoundedRectangle(roundedRect, strokeBrush, strokeStyle.Width, GetStrokeStyle(strokeStyle)); } } else { if (fillBrush != null) { Target.FillRectangle(rect, fillBrush); } if (strokeBrush != null) { Target.DrawRectangle(rect, strokeBrush, strokeStyle.Width, GetStrokeStyle(strokeStyle)); } } }
/// <summary> /// Fills the given rectangle with the given brush object. /// </summary> /// <param name="radiusX">The x radius of the rectangle's corners.</param> /// <param name="radiusY">The y radius of the rectangle's corners.</param> /// <param name="rectangle">The rectangle to be filled.</param> /// <param name="brush">The brush to be used.</param> public void FillRoundedRectangle(RectangleF rectangle, float radiusX, float radiusY, BrushResource brush) { if (m_renderTarget == null) { return; } rectangle.EnsureNotEmpty(nameof(rectangle)); brush.EnsureNotNull(nameof(brush)); radiusX.EnsurePositive(nameof(radiusX)); radiusY.EnsurePositive(nameof(radiusY)); D2D.RoundedRectangle roundedRect = new D2D.RoundedRectangle(); roundedRect.Rect = rectangle.ToDXRectangle(); roundedRect.RadiusX = radiusX; roundedRect.RadiusY = radiusY; m_renderTarget.FillRoundedRectangle( roundedRect, brush.GetBrush(m_device)); }
public void DrawRectangle(Rect frame, Size corner, Pen pen = null, Brush brush = null) { var p = GetBrush(pen); var b = GetBrush(frame, brush); if (b != null) { if (corner.Width > 0 || corner.Height > 0) { var rr = new D2D1.RoundedRectangle(); rr.Rect = frame.ToRectangleF(); rr.RadiusX = (float)corner.Width; rr.RadiusY = (float)corner.Height; renderTarget.FillRoundedRectangle(ref rr, b); } else { renderTarget.FillRectangle(frame.ToRectangleF(), b); } } if (p != null) { if (corner.Width > 0 || corner.Height > 0) { var rr = new D2D1.RoundedRectangle(); rr.Rect = frame.ToRectangleF(); rr.RadiusX = (float)corner.Width; rr.RadiusY = (float)corner.Height; renderTarget.DrawRoundedRectangle(ref rr, p, (float)pen.Width, GetStrokeStyle(pen)); } else { renderTarget.DrawRectangle(frame.ToRectangleF(), p, (float)pen.Width, GetStrokeStyle(pen)); } } }
/// <summary> /// Called when [render]. /// </summary> /// <param name="context">The context.</param> protected override void OnRender(RenderContext2D context) { var roundRect = new D2D.RoundedRectangle() { Rect = LayoutBound, RadiusX = CornerRadius, RadiusY = CornerRadius }; if (Background != null) { context.DeviceContext.FillRoundedRectangle(roundRect, Background); } if (!borderThickness.IsZero && StrokeBrush != null && StrokeStyle != null) { if (borderThickness.X == borderThickness.Y && borderThickness.X == borderThickness.Z && borderThickness.X == borderThickness.W) { context.DeviceContext.DrawRoundedRectangle(roundRect, StrokeBrush, borderThickness.X, StrokeStyle); } else { if (isBorderGeometryChanged) { var topLeft = LayoutBound.TopLeft + new Vector2(0, CornerRadius); var topRight = LayoutBound.TopRight - new Vector2(CornerRadius, 0); var bottomRight = LayoutBound.BottomRight - new Vector2(0, CornerRadius); var bottomLeft = LayoutBound.BottomLeft + new Vector2(CornerRadius, 0); if (borderThickness.X > 0) { var figures = new List <Figure>(); var figure = new Figure(topLeft, false, false); if (CornerRadius > 0) { figure.AddSegment(new ArcSegment(LayoutBound.TopLeft + new Vector2(CornerRadius, 0), new Size2F(CornerRadius, CornerRadius), 0, D2D.SweepDirection.Clockwise, D2D.ArcSize.Small)); } figure.AddSegment(new LineSegment(topRight)); figures.Add(figure); borderRenderCore[0].Figures = figures; borderRenderCore[0].StrokeWidth = borderThickness.X; } else { borderRenderCore[0].Figures = null; } if (borderThickness.Y > 0) { var figures = new List <Figure>(); var figure = new Figure(topRight, false, false); if (CornerRadius > 0) { figure.AddSegment(new ArcSegment(LayoutBound.TopRight + new Vector2(0, CornerRadius), new Size2F(CornerRadius, CornerRadius), 0, D2D.SweepDirection.Clockwise, D2D.ArcSize.Small)); } figure.AddSegment(new LineSegment(bottomRight)); figures.Add(figure); borderRenderCore[1].Figures = figures; borderRenderCore[1].StrokeWidth = borderThickness.Y; } else { borderRenderCore[1].Figures = null; } if (borderThickness.Z > 0) { var figures = new List <Figure>(); var figure = new Figure(bottomRight, false, false); if (CornerRadius > 0) { figure.AddSegment(new ArcSegment(LayoutBound.BottomRight - new Vector2(CornerRadius, 0), new Size2F(CornerRadius, CornerRadius), 0, D2D.SweepDirection.Clockwise, D2D.ArcSize.Small)); } figure.AddSegment(new LineSegment(bottomLeft)); figures.Add(figure); borderRenderCore[2].Figures = figures; borderRenderCore[2].StrokeWidth = borderThickness.Z; } else { borderRenderCore[2].Figures = null; } if (borderThickness.W > 0) { var figures = new List <Figure>(); var figure = new Figure(bottomLeft, false, false); if (CornerRadius > 0) { figure.AddSegment(new ArcSegment(LayoutBound.BottomLeft - new Vector2(0, CornerRadius), new Size2F(CornerRadius, CornerRadius), 0, D2D.SweepDirection.Clockwise, D2D.ArcSize.Small)); } figure.AddSegment(new LineSegment(topLeft)); figures.Add(figure); borderRenderCore[3].Figures = figures; borderRenderCore[3].StrokeWidth = borderThickness.W; } else { borderRenderCore[3].Figures = null; } isBorderGeometryChanged = false; } foreach (var core in borderRenderCore) { core.Transform = this.Transform; core.LocalTransform = this.LocalTransform; core.Render(context); } } } }
public void DrawRectangle (Rect frame, Size corner, Pen pen = null, Brush brush = null) { var p = GetBrush (pen); var b = GetBrush (frame, brush); if (b != null) { if (corner.Width > 0 || corner.Height > 0) { var rr = new D2D1.RoundedRectangle (); rr.Rect = frame.ToRectangleF (); rr.RadiusX = (float)corner.Width; rr.RadiusY = (float)corner.Height; renderTarget.FillRoundedRectangle (ref rr, b); } else { renderTarget.FillRectangle (frame.ToRectangleF (), b); } } if (p != null) { if (corner.Width > 0 || corner.Height > 0) { var rr = new D2D1.RoundedRectangle (); rr.Rect = frame.ToRectangleF (); rr.RadiusX = (float)corner.Width; rr.RadiusY = (float)corner.Height; renderTarget.DrawRoundedRectangle (ref rr, p, (float)pen.Width, GetStrokeStyle (pen)); } else { renderTarget.DrawRectangle (frame.ToRectangleF (), p, (float)pen.Width, GetStrokeStyle (pen)); } } }
public void DoRender(System.Drawing.Bitmap pic1, System.Drawing.Bitmap pic2, string addSong, bool IsPlaying) { try { InitSurfaceSize(); if (pic1 != null) { m_VideoBitmap = ConvertToSharpDXBitmap(m_D2DContext.d2dContext, pic1); pic1.Dispose(); int VideoW = (int)m_VideoBitmap.Size.Width; int VideoH = (int)m_VideoBitmap.Size.Height; if (SurfaceH2 > 0) { fontSize10 = (SurfaceH2) / 40; fontSize15 = (SurfaceH2) / 20; fontSize30 = (SurfaceH2) / 10; } else { //just guesing the font size fontSize10 = 10f; fontSize15 = 15f; fontSize30 = 30f; } LoadResources(); m_D2DContext.d2dContextCdgText.Target = m_Bitmap1; // associate bitmap with the d2d context m_D2DContext.d2dContextCdgText.BeginDraw(); m_D2DContext.d2dContextCdgText.Clear(Color.Transparent); m_D2DContext.d2dContextCdgText2.Target = m_Bitmap1FullScreen; // associate bitmap with the d2d context m_D2DContext.d2dContextCdgText2.BeginDraw(); m_D2DContext.d2dContextCdgText2.Clear(Color.Transparent); m_Shadow.SetInput(0, m_Bitmap1, true); // Create image shadow effect m_ShadowFullScreen.SetInput(0, m_Bitmap1FullScreen, true); m_ShadowFullScreen.Optimization = D2D1.ShadowOptimization.Quality; // Create image transform effect m_affineTransformEffect.SetInputEffect(0, m_ShadowFullScreen); m_affineTransformEffect.TransformMatrix = Matrix3x2.Translation(0, 0); // Create composite effect // m_compositeEffect.InputCount = 2; m_compositeEffect.SetInputCount(2); m_compositeEffect.SetInputEffect(0, m_ShadowFullScreen); m_compositeEffect.SetInputEffect(1, m_affineTransformEffect); m_compositeEffect.SetInput(2, m_Bitmap1FullScreen, true); if (pic2 != null) { m_CDGBitmap = ConvertToSharpDXBitmap(m_D2DContext.d2dContextCdgText, pic2); pic2.Dispose(); //float Height = (SurfaceH * 2) / 3f; //float Width = (SurfaceW * 2) / 3f; float Height2 = (SurfaceH2 * 2) / 3f; float Width2 = (SurfaceW2 * 2) / 3f; float CdgW = (m_CDGBitmap.Size.Width * 2) / 3f; float CdgH = (m_CDGBitmap.Size.Height * 2) / 3f; // float CdgWFullScreen = (m_CDGBitmap.Size.Width + SurfaceW2) / 2f; // float CdgHFullScreen = (m_CDGBitmap.Size.Height + SurfaceH2) / 2f; m_destCdgRectangle = new RawRectangleF((SurfaceR / 2) - (CdgW / 2), SurfaceH, (SurfaceR / 2) + (CdgW / 2), SurfaceB - CdgH); m_destCdgRectangle2 = new RawRectangleF(((SurfaceR2 / 2) - (Width2 / 2)), SurfaceH2, ((SurfaceR2 / 2) + (Width2 / 2)), SurfaceB2 - Height2); m_D2DContext.d2dContextCdgText.DrawBitmap(m_CDGBitmap, m_destCdgRectangle, 1.0f, D2D1.BitmapInterpolationMode.Linear); m_D2DContext.d2dContextCdgText2.DrawBitmap(m_CDGBitmap, m_destCdgRectangle2, 1.0f, D2D1.BitmapInterpolationMode.Linear); } m_videoBitmapRectangle = new RawRectangleF((SurfaceR / 2) - (VideoW / 6), 0, (SurfaceR / 2) + (VideoW / 6), SurfaceH); m_D2DContext.d2dContextCdgText.EndDraw(); m_D2DContext.d2dContextCdgText2.EndDraw(); m_D2DContext.d2dContext.BeginDraw(); m_D2DContext.d2dContext.Clear(Color.Transparent); m_D2DContext.d2dContext.DrawBitmap(m_VideoBitmap, m_videoBitmapRectangle, 1.0f, D2D1.BitmapInterpolationMode.Linear); if (!IsPlaying) { m_TitleName = DrawString(VideoW, VideoH, fontSize15, fontSize30); m_D2DContext.d2dContext.DrawBitmap(m_TitleName, new RawRectangleF((SurfaceR / 2) - (VideoW / 4), 0, (SurfaceR / 2) + (VideoW / 4), SurfaceH), 1.0f, D2D1.BitmapInterpolationMode.Linear); } if (PlayerControl.IsAddToReserve) { m_D2DContext.d2dContext.DrawRectangle(m_videoBitmapRectangle, roundedRecInColor, 10); } m_D2DContext.d2dContext.DrawImage(m_Shadow, new RawVector2(0, 0), D2D1.InterpolationMode.Linear, D2D1.CompositeMode.Xor); m_D2DContext.d2dContext.DrawBitmap(m_Bitmap1, 1f, D2D1.BitmapInterpolationMode.Linear); m_D2DContext.d2dContext.EndDraw(); m_D2DContext.swapChain.Present(0, DXGI.PresentFlags.None); m_D2DContext.d2dContext2.BeginDraw(); m_D2DContext.d2dContext2.Clear(Color.Transparent); m_D2DContext.d2dContext2.DrawBitmap(m_VideoBitmap, new RawRectangleF(SurfaceL2, SurfaceT2, SurfaceR2, SurfaceH2), 1.0f, D2D1.BitmapInterpolationMode.Linear); if (!IsPlaying) { //m_D2DContext.d2dContext2.DrawBitmap(m_TitleName, new RawRectangleF(SurfaceL2, SurfaceT2, SurfaceR2, SurfaceH2), 1.0f, D2D1.BitmapInterpolationMode.Linear); m_D2DContext.d2dContext2.DrawBitmap(m_TitleName, new RawRectangleF((SurfaceR2 / 2) - (VideoW / 2), 0, (SurfaceR2 / 2) + (VideoW / 2), SurfaceH2), 1.0f, D2D1.BitmapInterpolationMode.Linear); } else { string reservedSong = "R".PadRight(2) + addSong; var stringSize = MeasureStringDX(reservedSong, SurfaceW2, m_textFormat15); // m_roundedRec = new D2D1.RoundedRectangle() { Rect = new RawRectangleF(SurfaceW2 - stringSize.Width, (stringSize.Height / 3), (SurfaceR2 - 10), stringSize.Height + (stringSize.Height / 3)), RadiusX = stringSize.Height / 8, RadiusY = stringSize.Height / 8 }; m_roundedRec = new D2D1.RoundedRectangle() { Rect = new RawRectangleF((SurfaceW2 - 10) - (stringSize.Width + 10), (SurfaceT2 + 10), (SurfaceR2 - 10), (stringSize.Height + 10)), RadiusX = stringSize.Height / 8, RadiusY = stringSize.Height / 8 }; m_D2DContext.d2dContext2.DrawRoundedRectangle(m_roundedRec, roundedRecOutColor, 10f); m_D2DContext.d2dContext2.FillRoundedRectangle(m_roundedRec, roundedRecInColor); // m_D2DContext.d2dContext2.DrawText(reservedSong, m_textFormat15, new RawRectangleF(m_roundedRec.Rect.Left + (stringSize.Width / 12), (stringSize.Height / 2.5f), SurfaceR2, stringSize.Height + (stringSize.Height / 3)), m_textBrush); m_D2DContext.d2dContext2.DrawText(reservedSong, m_textFormat15, new RawRectangleF((m_roundedRec.Rect.Left + 5), (m_roundedRec.Rect.Top - 2), SurfaceR2, m_roundedRec.Rect.Bottom), m_textBrush); if (PlayerControl.GetNextSong != "") { string nextSong = "Next song: " + PlayerControl.GetNextSong; var stringSize2 = MeasureStringDX(nextSong, SurfaceW2, m_textFormat10); m_roundedRecNextSong = new D2D1.RoundedRectangle() { Rect = new RawRectangleF((SurfaceL2 + 10), (SurfaceT2 + 10), (m_roundedRec.Rect.Left - 15), (stringSize.Height + 10)), RadiusX = stringSize2.Height / 8, RadiusY = stringSize2.Height / 8 }; m_D2DContext.d2dContext2.DrawRoundedRectangle(m_roundedRecNextSong, roundedRecOutColor, 10f); m_D2DContext.d2dContext2.FillRoundedRectangle(m_roundedRecNextSong, roundedRecInColor); // m_D2DContext.d2dContext2.DrawText(nextSong, m_textFormat10, new RawRectangleF((m_roundedRecNextSong.Rect.Right / 2) - (stringSize2.Width / 2), (m_roundedRecNextSong.Rect.Bottom / 2) - ((stringSize2.Height - 10) / 2), (SurfaceR2 - 10), m_roundedRecNextSong.Rect.Bottom), m_textBrush, D2D1.DrawTextOptions.Clip); m_D2DContext.d2dContext2.DrawText(nextSong, m_textFormat10, new RawRectangleF((SurfaceL2 + 10), (SurfaceT2 + 10), (m_roundedRec.Rect.Left - 15), (stringSize.Height + 10)), m_textBrush, D2D1.DrawTextOptions.Clip); } } m_D2DContext.d2dContext2.DrawImage(m_compositeEffect, new RawVector2(0, 0), D2D1.InterpolationMode.Linear, D2D1.CompositeMode.Xor); m_D2DContext.d2dContext2.DrawBitmap(m_Bitmap1FullScreen, 1f, D2D1.BitmapInterpolationMode.Linear); m_D2DContext.d2dContext2.EndDraw(); m_D2DContext.swapChain2.Present(0, DXGI.PresentFlags.None); UnloadResources(); } } catch (Exception ex) { Logger.LogFile(ex.Message, "", "DoRender", ex.LineNumber(), "GraphicUtilDX"); } }