void FillIn2(D2DGraphics g, Color boxColor) { RawRectangleF rect = new RawRectangleF(0, 0, Width, Height); RadialGradientBrushProperties props2 = new RadialGradientBrushProperties() { Center = new RawVector2((rect.Left + rect.Right) / 2, (rect.Top + rect.Bottom) / 2), GradientOriginOffset = new RawVector2(0, 0), RadiusX = Width / 2, RadiusY = Height / 2 }; GradientStop[] gradientStops2 = new GradientStop[2]; gradientStops2[0].Color = GDIDataD2DUtils.TransToRawColor4(Color.FromArgb(55, boxColor.R, boxColor.G, boxColor.B)); gradientStops2[0].Position = 0f; //gradientStops2[1].Color = GDIDataD2DUtils.TransToRawColor4(Color.FromArgb(40, 0,0,255)); //gradientStops2[1].Position = 0.5f; gradientStops2[1].Color = GDIDataD2DUtils.TransToRawColor4(Color.FromArgb(55, boxColor.R, boxColor.G, boxColor.B)); gradientStops2[1].Position = 1f; // GradientStopCollection gradientStopCollection2 = new GradientStopCollection(g.RenderTarget, gradientStops2, Gamma.StandardRgb, ExtendMode.Clamp); RadialGradientBrush radialGradientBrush = new RadialGradientBrush(g.RenderTarget, props2, gradientStopCollection2); g.RenderTarget.FillRectangle(rect, radialGradientBrush); gradientStopCollection2.Dispose(); radialGradientBrush.Dispose(); }
protected override void OnCleanUpDeviceResources() { base.OnCleanUpDeviceResources(); _radialGradientBrush.Dispose(); _sceneBrush.Dispose(); _gridPatternBrush.Dispose(); }
private void Dispose(bool disposing) { if (_canvasRadialGradientBrush != null) { _canvasRadialGradientBrush.Dispose(); _canvasRadialGradientBrush = null; } }
public void Dispose() { if (Brush != null && !Brush.Disposed) { Brush.Dispose(); } GC.SuppressFinalize(this); }
/// <summary> /// Dispose all the created objects. /// </summary> public void Uninitialize() { radialGradientBrush.Dispose(); linearGradientBrush.Dispose(); solidBrush.Dispose(); swapChain.Dispose(); d2dTarget.Dispose(); d3dContext.Dispose(); d2dContext.Dispose(); device.Dispose(); }
/// <summary> /// Creates new brushes in order to perform animations on the individual gradient based squares. /// </summary> /// <param name="direct2DWindowTarget">The object we use to draw! Our device! Our soul!</param> private static void AnimateBrushGradient(RenderTarget direct2DWindowTarget) { // Animate the gradientstops. for (int x = 0; x < _gradientStops.Length; x++) { float currentPosition = _gradientStops[x].Position; if (currentPosition >= 1F) { currentPosition = 0F; } else { currentPosition += 0.01F; } _gradientStops[x].Position = currentPosition; } // Conditionally dispose. _squareGradientBrush?.Factory?.Dispose(); _squareGradientBrush?.GradientStopCollection?.Dispose(); _squareGradientBrush?.Dispose(); _squareRadialGradientBrush?.Factory?.Dispose(); _squareRadialGradientBrush?.GradientStopCollection?.Dispose(); _squareRadialGradientBrush?.Dispose(); // Fix memory leak with GradientStopCollection // Thanks, anonymous StackOverflow user! https://stackoverflow.com/questions/47876001/mysterious-direct2d-memory-leak-with-gradients using (var gradientStopCollection = new GradientStopCollection(direct2DWindowTarget, _gradientStops)) { /* How about a brush with a gradient? */ LinearGradientBrushProperties linearGradientBrushProperties = new LinearGradientBrushProperties() { StartPoint = new RawVector2(_gradientRectangle.Top - 50, _gradientRectangle.Left - 50), // Note: Physical location of the start point, in this case top left corner. EndPoint = new RawVector2(_gradientRectangle.Bottom + 50, _gradientRectangle.Right + 50) // Note: Physical location of the start point, in this case bottom right corner. }; _squareGradientBrush = new LinearGradientBrush(direct2DWindowTarget, linearGradientBrushProperties, gradientStopCollection); /* A radial brush? */ RadialGradientBrushProperties radialGradientBrushProperties = new RadialGradientBrushProperties() { Center = new RawVector2(_radialRectangle.Left + 50, _radialRectangle.Top + 50), // Note: Physical location of the start point, in this case the actual center. GradientOriginOffset = new RawVector2(0, 0), RadiusX = 100, RadiusY = 100 }; _squareRadialGradientBrush = new RadialGradientBrush(direct2DWindowTarget, radialGradientBrushProperties, gradientStopCollection); } }