/// <summary> /// Initializes a new LinearGradientBrush using the target device and an Color[]. /// </summary> /// <param name="device">The Graphics device.</param> /// <param name="colors">The colors</param> public LinearGradientBrush(RenderTarget device, params Color[] colors) { if (device == null) { throw new ArgumentNullException(nameof(device)); } if (colors == null || colors.Length == 0) { throw new ArgumentNullException(nameof(colors)); } float position = 0.0f; float stepSize = 1.0f / colors.Length; var gradientStops = new GradientStop[colors.Length]; for (int i = 0; i < colors.Length; i++) { gradientStops[i] = new GradientStop() { Color = colors[i], Position = position }; position += stepSize; } Brush = new SharpDXGradientBrush(device, new LinearGradientBrushProperties(), new GradientStopCollection(device, gradientStops, ExtendMode.Clamp)); }
private void Selector_D2DPaint(D2DGraphics g) { if (!IsSelected) { return; } g.RenderTarget.AntialiasMode = AntialiasMode.Aliased; RawRectangleF rect = new RawRectangleF(1, 1, selector.Width - 2, selector.Height - 9); RawColor4 rawColor = GDIDataD2DUtils.TransToRawColor4(SelectorColor); SolidColorBrush brush = new SolidColorBrush(g.RenderTarget, rawColor); RoundedRectangle rounderRect = new RoundedRectangle { RadiusX = 1, RadiusY = 1, Rect = rect }; g.RenderTarget.DrawRoundedRectangle(rounderRect, brush, 2f); brush.Dispose(); // GradientStop[] gradientStops = new GradientStop[2]; gradientStops[0].Color = new RawColor4(0, 0, 0, 0.1f); gradientStops[0].Position = 0f; gradientStops[1].Color = new RawColor4(0, 0, 0, 0); gradientStops[1].Position = 1f; // GradientStopCollection gradientStopCollection = new GradientStopCollection(g.RenderTarget, gradientStops, Gamma.StandardRgb, ExtendMode.Clamp); // rect = new RawRectangleF(0, selector.Height - 9, selector.Width - 1, selector.Height - 1); LinearGradientBrushProperties props = new LinearGradientBrushProperties() { StartPoint = new RawVector2(rect.Left, rect.Top), EndPoint = new RawVector2(rect.Left, rect.Bottom) }; SharpDX.Direct2D1.LinearGradientBrush linearGradientBrush = new SharpDX.Direct2D1.LinearGradientBrush(g.RenderTarget, props, gradientStopCollection); g.RenderTarget.FillRectangle(rect, linearGradientBrush); gradientStopCollection.Dispose(); linearGradientBrush.Dispose(); //Graphics gdiGraphics = g.CreateGdiGraphics(); //gdiGraphics.SmoothingMode = SmoothingMode.HighQuality; //RectangleF rect1 = new RectangleF(0, 0, (float)Math.Ceiling(Width - 1), (float)Math.Ceiling(Height - 1)); //Pen pen = new Pen(Color.FromArgb(255, 191, 152, 90), 1f); //DrawUtils.DrawRoundRectangle(gdiGraphics, Pens.Gold, rect1, 4); //g.RelaseGdiGraphics(gdiGraphics); //pen.Dispose(); }
public static LinearGradientBrush New(string name, Direct2DDevice device, LinearGradient linearGradient) { using (var d2dGradientStopCollection = new SharpDX.Direct2D1.GradientStopCollection(device, linearGradient.GradientStops.Select(gs => (SharpDX.Direct2D1.GradientStop)gs).ToArray(), (SharpDX.Direct2D1.ExtendMode)linearGradient.GradientStops.ExtendMode)) { var lgbProperties = new LinearGradientBrushProperties() { StartPoint = linearGradient.StartPoint, EndPoint = linearGradient.EndPoint }; var brushProperties = new BrushProperties() { Opacity = linearGradient.Opacity }; var brush = new SharpDX.Direct2D1.LinearGradientBrush(device, lgbProperties, brushProperties, d2dGradientStopCollection); return(new LinearGradientBrush(name, device, linearGradient, brush)); } }
internal override void Create() { if (NativeBrush != null) NativeBrush.Dispose(); var stops = new GradientStop[m_gradientStops.Length]; for (int s = 0; s < m_gradientStops.Length; s++) { stops[s].Color = m_gradientStops[s].Color.ToColor4(); stops[s].Position = m_gradientStops[s].Position; } using (var stopcol = new GradientStopCollection(Owner.D2dRenderTarget, stops, (Gamma)m_gamma, (ExtendMode)m_extendMode)) { var props = new LinearGradientBrushProperties(); props.StartPoint = new DrawingPointF(m_start.X, m_start.Y); props.EndPoint = new DrawingPointF(m_end.X, m_end.Y); NativeBrush = new LinearGradientBrush(Owner.D2dRenderTarget, props, stopcol); } }
void FillIn(D2DGraphics g, Color boxColor) { RawRectangleF rect = new RawRectangleF(FillMargin.left + 1, FillMargin.top + 1, Width - FillMargin.right, Height - FillMargin.bottom); RawColor4 rawColor = GDIDataD2DUtils.TransToRawColor4(boxColor); SolidColorBrush brush = new SolidColorBrush(g.RenderTarget, rawColor); g.RenderTarget.DrawRectangle(rect, brush, 1); brush.Dispose(); // RawRectangleF fillRect = new RawRectangleF(FillMargin.left, FillMargin.top, Width - FillMargin.right, Height - FillMargin.bottom); g.RenderTarget.AntialiasMode = AntialiasMode.PerPrimitive; GradientStop[] gradientStops = new GradientStop[3]; gradientStops[0].Color = GDIDataD2DUtils.TransToRawColor4(Color.FromArgb(255, boxColor.R, boxColor.G, boxColor.B)); gradientStops[0].Position = 0f; gradientStops[1].Color = GDIDataD2DUtils.TransToRawColor4(Color.FromArgb(20, boxColor.R, boxColor.G, boxColor.B)); gradientStops[1].Position = 0.5f; gradientStops[2].Color = GDIDataD2DUtils.TransToRawColor4(Color.FromArgb(0, boxColor.R, boxColor.G, boxColor.B)); gradientStops[2].Position = 1f; // GradientStopCollection gradientStopCollection = new GradientStopCollection(g.RenderTarget, gradientStops, Gamma.StandardRgb, ExtendMode.Clamp); LinearGradientBrushProperties props = new LinearGradientBrushProperties() { StartPoint = new RawVector2(fillRect.Left, fillRect.Top), EndPoint = new RawVector2(fillRect.Right, fillRect.Bottom) }; SharpDX.Direct2D1.LinearGradientBrush linearGradientBrush = new SharpDX.Direct2D1.LinearGradientBrush(g.RenderTarget, props, gradientStopCollection); g.RenderTarget.FillRectangle(fillRect, linearGradientBrush); gradientStopCollection.Dispose(); linearGradientBrush.Dispose(); }
private LinearGradientBrush GetCachedLinearGradientBrush(Color color1, Color color2) { long kh = color1.ToArgb(); // high bits long kl = color2.ToArgb(); // low bits var colorKey = (kh << 32) | kl; LinearGradientBrush brush; if (!m_linearGradients.TryGetValue(colorKey, out brush)) { var stops = new GradientStop[2]; stops[0].Color = color1.ToColor4(); stops[0].Position = 0; stops[1].Color = color2.ToColor4(); stops[1].Position = 1; using (var stopCollection = new GradientStopCollection(m_renderTarget, stops, Gamma.StandardRgb, ExtendMode.Clamp)) { var props = new LinearGradientBrushProperties(); brush = new LinearGradientBrush(m_renderTarget, props, stopCollection); } m_linearGradients.Add(colorKey, brush); } return brush; }
/// <summary> /// Now that we have a CoreWindow object, the DirectX device/context can be created. /// </summary> /// <param name="entryPoint"></param> public void Load(string entryPoint) { // Get the default hardware device and enable debugging. Don't care about the available feature level. // DeviceCreationFlags.BgraSupport must be enabled to allow Direct2D interop. SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport); // Query the default device for the supported device and context interfaces. device = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>(); d3dContext = device.ImmediateContext.QueryInterface<SharpDX.Direct3D11.DeviceContext1>(); // Query for the adapter and more advanced DXGI objects. SharpDX.DXGI.Device2 dxgiDevice2 = device.QueryInterface<SharpDX.DXGI.Device2>(); SharpDX.DXGI.Adapter dxgiAdapter = dxgiDevice2.Adapter; SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>(); // Description for our swap chain settings. SwapChainDescription1 description = new SwapChainDescription1() { // 0 means to use automatic buffer sizing. Width = 0, Height = 0, // 32 bit RGBA color. Format = Format.B8G8R8A8_UNorm, // No stereo (3D) display. Stereo = false, // No multisampling. SampleDescription = new SampleDescription(1, 0), // Use the swap chain as a render target. Usage = Usage.RenderTargetOutput, // Enable double buffering to prevent flickering. BufferCount = 2, // No scaling. Scaling = Scaling.None, // Flip between both buffers. SwapEffect = SwapEffect.FlipSequential, }; // Generate a swap chain for our window based on the specified description. swapChain = dxgiFactory2.CreateSwapChainForCoreWindow(device, new ComObject(window), ref description, null); // Get the default Direct2D device and create a context. SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None); // Specify the properties for the bitmap that we will use as the target of our Direct2D operations. // We want a 32-bit BGRA surface with premultiplied alpha. BitmapProperties1 properties = new BitmapProperties1(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), DisplayProperties.LogicalDpi, DisplayProperties.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw); // Get the default surface as a backbuffer and create the Bitmap1 that will hold the Direct2D drawing target. Surface backBuffer = swapChain.GetBackBuffer<Surface>(0); d2dTarget = new Bitmap1(d2dContext, backBuffer, properties); // Create a solid color brush. solidBrush = new SolidColorBrush(d2dContext, Color.Coral); // Create a linear gradient brush. // Note that the StartPoint and EndPoint values are set as absolute coordinates of the surface you are drawing to, // NOT the geometry we will apply the brush. linearGradientBrush = new LinearGradientBrush(d2dContext, new LinearGradientBrushProperties() { StartPoint = new Vector2(50, 0), EndPoint = new Vector2(450, 0), }, new GradientStopCollection(d2dContext, new GradientStop[] { new GradientStop() { Color = Color.Blue, Position = 0, }, new GradientStop() { Color = Color.Green, Position = 1, } })); // Create a radial gradient brush. // The center is specified in absolute coordinates, too. radialGradientBrush = new RadialGradientBrush(d2dContext, new RadialGradientBrushProperties() { Center = new Vector2(250, 525), RadiusX = 100, RadiusY = 100, }, new GradientStopCollection(d2dContext, new GradientStop[] { new GradientStop() { Color = Color.Yellow, Position = 0, }, new GradientStop() { Color = Color.Red, Position = 1, } })); }
private LinearGradientBrush(string name, Direct2DDevice device, LinearGradient linearGradient, SharpDX.Direct2D1.LinearGradientBrush brush) : base(name, device, linearGradient, brush) { resource = brush; colorResource = linearGradient; }
void FillItemGeometry(D2DGraphics g) { g.RenderTarget.AntialiasMode = AntialiasMode.PerPrimitive; RawRectangleF rect = new RawRectangleF(1, 1, Width - 1, Height - 1); RoundedRectangle roundedRect = new RoundedRectangle() { RadiusX = this.RadiusX, RadiusY = this.RadiusY, Rect = rect }; GradientStop[] gradientStops = new GradientStop[5]; gradientStops[0].Color = GDIDataD2DUtils.TransToRawColor4(colors[0]); gradientStops[0].Position = 0f; gradientStops[1].Color = GDIDataD2DUtils.TransToRawColor4(colors[1]); gradientStops[1].Position = 0.4f; gradientStops[2].Color = GDIDataD2DUtils.TransToRawColor4(colors[2]); gradientStops[2].Position = 0.5f; gradientStops[3].Color = GDIDataD2DUtils.TransToRawColor4(colors[1]); gradientStops[3].Position = 0.6f; gradientStops[4].Color = GDIDataD2DUtils.TransToRawColor4(colors[0]); gradientStops[4].Position = 1f; // GradientStopCollection gradientStopCollection = new GradientStopCollection(g.RenderTarget, gradientStops, Gamma.StandardRgb, ExtendMode.Clamp); // LinearGradientBrushProperties props = new LinearGradientBrushProperties() { StartPoint = new RawVector2(rect.Left, rect.Top), EndPoint = new RawVector2(rect.Left, rect.Bottom) }; SharpDX.Direct2D1.LinearGradientBrush linearGradientBrush = new SharpDX.Direct2D1.LinearGradientBrush(g.RenderTarget, props, gradientStopCollection); g.RenderTarget.FillRoundedRectangle(roundedRect, linearGradientBrush); // gradientStops = new GradientStop[5]; gradientStops[0].Color = GDIDataD2DUtils.TransToRawColor4(colors[2]); gradientStops[0].Position = 0f; gradientStops[1].Color = GDIDataD2DUtils.TransToRawColor4(colors[1]); gradientStops[1].Position = 0.1f; gradientStops[2].Color = GDIDataD2DUtils.TransToRawColor4(colors[0]); gradientStops[2].Position = 0.5f; gradientStops[3].Color = GDIDataD2DUtils.TransToRawColor4(colors[1]); gradientStops[3].Position = 0.9f; gradientStops[4].Color = GDIDataD2DUtils.TransToRawColor4(colors[2]); gradientStops[4].Position = 1f; // gradientStopCollection = new GradientStopCollection(g.RenderTarget, gradientStops, Gamma.StandardRgb, ExtendMode.Clamp); // props = new LinearGradientBrushProperties() { StartPoint = new RawVector2(rect.Left, rect.Top), EndPoint = new RawVector2(rect.Right, rect.Top) }; linearGradientBrush = new SharpDX.Direct2D1.LinearGradientBrush(g.RenderTarget, props, gradientStopCollection); g.RenderTarget.FillRoundedRectangle(roundedRect, linearGradientBrush); RawColor4 rawColor = GDIDataD2DUtils.TransToRawColor4(Color.FromArgb(SideShadowAlpha, 0, 0, 0)); SolidColorBrush brush = new SolidColorBrush(g.RenderTarget, rawColor); g.RenderTarget.DrawRoundedRectangle(roundedRect, brush); }
public static byte[] CreatePngImage(int width, int height, string text, float fontSize = 30.0f, string font = "Times New Roman", int lineCount = 5, bool rotation = false, float turbulenceAmount = 60.0f) { using (var wic = new WIC.ImagingFactory2()) using (var d2d = new D2D.Factory()) using (var wicBitmap = new WIC.Bitmap(wic, width, height, WIC.PixelFormat.Format32bppPBGRA, WIC.BitmapCreateCacheOption.CacheOnDemand)) using (var target = new D2D.WicRenderTarget(d2d, wicBitmap, new D2D.RenderTargetProperties())) using (var dwriteFactory = new DWrite.Factory()) using (var brush = new D2D.SolidColorBrush(target, Color.Yellow)) using (var encoder = new WIC.PngBitmapEncoder(wic)) using (var ms = new MemoryStream()) using (var dc = target.QueryInterface <D2D.DeviceContext>()) using (var bmpLayer = new D2D.Bitmap1(dc, target.PixelSize, new D2D.BitmapProperties1(new D2D.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied), d2d.DesktopDpi.Width, d2d.DesktopDpi.Height, D2D.BitmapOptions.Target))) { var r = new Random(); encoder.Initialize(ms); D2D.Image oldTarget = dc.Target; { dc.Target = bmpLayer; dc.BeginDraw(); var textFormat = new DWrite.TextFormat(dwriteFactory, font, fontSize); for (int charIndex = 0; charIndex < text.Length; ++charIndex) { using (var layout = new DWrite.TextLayout(dwriteFactory, text[charIndex].ToString(), textFormat, float.MaxValue, float.MaxValue)) { var layoutSize = new Vector2(layout.Metrics.Width, layout.Metrics.Height); using (var b2 = new D2D.LinearGradientBrush(dc, new D2D.LinearGradientBrushProperties { StartPoint = Vector2.Zero, EndPoint = layoutSize, }, new D2D.GradientStopCollection(dc, new[] { new D2D.GradientStop { Position = 0.0f, Color = ColorFromHsl(r.NextFloat(0, 1), 1.0f, 0.8f) }, new D2D.GradientStop { Position = 1.0f, Color = ColorFromHsl(r.NextFloat(0, 1), 1.0f, 0.8f) }, }))) { var position = new Vector2(charIndex * width / text.Length, r.NextFloat(0, height - layout.Metrics.Height)); dc.Transform = Matrix3x2.Translation(-layoutSize / 2) * Matrix3x2.Skew(r.NextFloat(0, 0.5f), r.NextFloat(0, 0.5f)) * (rotation ? Matrix3x2.Rotation(r.NextFloat(0, (float)(Math.PI * 2))) : Matrix3x2.Identity) * Matrix3x2.Translation(position + layoutSize / 2); dc.DrawTextLayout(Vector2.Zero, layout, b2); } } } for (var i = 0; i < lineCount; ++i) { target.Transform = Matrix3x2.Identity; brush.Color = ColorFromHsl(r.NextFloat(0, 1), 1.0f, 0.3f); target.DrawLine( r.NextVector2(Vector2.Zero, new Vector2(width, height)), r.NextVector2(Vector2.Zero, new Vector2(width, height)), brush, 3.0f); } target.EndDraw(); } Color background = ColorFromHsl(r.NextFloat(0, 1), 1.0f, 0.3f); { dc.Target = null; using (var displacement = new D2D.Effects.DisplacementMap(dc)) { displacement.SetInput(0, bmpLayer, true); displacement.Scale = turbulenceAmount; var turbulence = new D2D.Effects.Turbulence(dc); displacement.SetInputEffect(1, turbulence); dc.Target = oldTarget; dc.BeginDraw(); dc.Clear(background); dc.DrawImage(displacement); dc.EndDraw(); using (var frame = new WIC.BitmapFrameEncode(encoder)) { frame.Initialize(); frame.SetSize(wicBitmap.Size.Width, wicBitmap.Size.Height); var pixelFormat = wicBitmap.PixelFormat; frame.SetPixelFormat(ref pixelFormat); frame.WriteSource(wicBitmap); frame.Commit(); } } } encoder.Commit(); return(ms.ToArray()); } }