/// <summary> /// Gets the input object for an effect. /// </summary> /// <param name="device">The device for which to get the input.</param> IDisposable IImageInternal.GetImageObject(EngineDevice device) { if (device.IsUsingFallbackMethodFor2D) { return(null); } D2D.Effect effect = m_loadedEffects[device.DeviceIndex]; if (effect == null) { // Create the effect effect = BuildEffect(device); // Set input values for (int loop = 0; loop < m_effectInputs.Length; loop++) { using (D2D.Image actInput = m_effectInputs[loop].GetImageObject(device) as D2D.Image) { effect.SetInput(loop, actInput, new SharpDX.Mathematics.Interop.RawBool(false)); } } // Store loaded effect m_loadedEffects[device.DeviceIndex] = effect; } return(effect.Output); }
public override void Render() { if (Source.Updated) { _Pelete?.Dispose(); _Pelete = Source.Output; } using (Image PrepairedImage = Output(HostDC)) HostDC.DrawImage(PrepairedImage, new RawVector2(_Position.X, _Position.Y), null, SharpDX.Direct2D1.InterpolationMode.Linear, CompositeMode.SourceOver); }
/// <inheritdoc /> /// <summary> /// Refreshes the control /// </summary> internal override void Refresh() { if (this.invert && Bitmap != null) { var effect = new Effect(Toolbar.ToolbarRenderTarget.QueryInterface <DeviceContext>(), Effect.Invert); effect.SetInput(0, Bitmap, true); this.image = effect.Output; this.imageSize = Bitmap.Size; Bitmap?.Dispose(); Bitmap = null; } base.Refresh(); }
/// <summary> /// Draws the given image. /// </summary> /// <param name="image">The source of pixel data to be rendered.</param> /// <param name="destinationOrigin">The origin point where to draw the image.</param> public void DrawImage( IImage image, Vector2 destinationOrigin) { if (m_renderTarget == null) { return; } image.EnsureNotNull(nameof(image)); IImageInternal internalImage = image as IImageInternal; internalImage.EnsureNotNull(nameof(internalImage)); if (m_deviceContext != null) { D2D.Image d2dImage = internalImage.GetImageObject(m_device) as D2D.Image; d2dImage.EnsureNotNull(nameof(d2dImage)); m_deviceContext.DrawImage( d2dImage, destinationOrigin.ToDXVector(), null, D2D.InterpolationMode.Linear, D2D.CompositeMode.SourceOver); } else { BitmapResource bitmap = internalImage.TryGetSourceBitmap(); if (bitmap != null) { this.DrawBitmap(bitmap, destinationOrigin); } } }
/// <summary> /// Set the input. /// </summary> /// <param name="input">The input image.</param> public virtual void SetInput(D2D.Image input) { this.Effects.FirstOrDefault()?.SetInput(0, input, false); this.CompositeEffect.SetInput(1, input, false); }
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()); } }