/// <summary> /// 从指定的控件里面创建设备资源。 /// </summary> /// <param name="target">用于创建设备资源的控件。</param> public void CreateDeviceResource(Control target) { if (_renderTarget == null) { _renderTarget = _d2dFactory.CreateHwndRenderTarget( new RenderTargetProperties(), new HwndRenderTargetProperties( target.Handle, new SizeU((uint)target.Width, (uint)target.Height), PresentOptions.None ) ); } }
/// <summary> /// This method creates the render target and associated D2D and DWrite resources /// </summary> void CreateDeviceResources() { // Only calls if resources have not been initialize before if (renderTarget == null) { // Create the render target SizeU size = new SizeU((uint)renderControl.ClientSize.Width, (uint)renderControl.ClientSize.Height); HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(renderControl.Handle, size, PresentOptions.RetainContents); renderTarget = d2dFactory.CreateHwndRenderTarget(renderProps, hwndProps); // Create an initial black brush brushes.Add(renderTarget.CreateSolidColorBrush(new ColorF(Color.Black.ToArgb()))); currentBrushIndex = 0; } }
/// <summary> /// This method creates the render target and all associated D2D and DWrite resources /// </summary> void CreateDeviceResources() { // Only calls if resources have not been initialize before if (renderTarget == null) { // The text format textFormat = dwriteFactory.CreateTextFormat("Bodoni MT", 24, DWrite.FontWeight.Normal, DWrite.FontStyle.Italic, DWrite.FontStretch.Normal); // Create the render target SizeU size = new SizeU((uint)host.ActualWidth, (uint)host.ActualHeight); RenderTargetProperties props = new RenderTargetProperties(); HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(host.Handle, size, PresentOptions.None); renderTarget = d2dFactory.CreateHwndRenderTarget(props, hwndProps); // A black brush to be used for drawing text ColorF cf = new ColorF(0, 0, 0, 1); blackBrush = renderTarget.CreateSolidColorBrush(cf); // Create a linear gradient. GradientStop[] stops = { new GradientStop(1, new ColorF(1f, 0f, 0f, 0.25f)), new GradientStop(0, new ColorF(0f, 0f, 1f, 1f)) }; GradientStopCollection pGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap); LinearGradientBrushProperties gradBrushProps = new LinearGradientBrushProperties(new Point2F(50, 25), new Point2F(25, 50)); linearGradientBrush = renderTarget.CreateLinearGradientBrush(gradBrushProps, pGradientStops); gridPatternBitmapBrush = CreateGridPatternBrush(renderTarget); solidBrush1 = renderTarget.CreateSolidColorBrush(new ColorF(0.3F, 0.5F, 0.65F, 0.25F)); solidBrush2 = renderTarget.CreateSolidColorBrush(new ColorF(0.0F, 0.0F, 0.65F, 0.5F)); solidBrush3 = renderTarget.CreateSolidColorBrush(new ColorF(0.9F, 0.5F, 0.3F, 0.75F)); // Create a linear gradient. stops[0] = new GradientStop(1, new ColorF(0f, 0f, 0f, 0.25f)); stops[1] = new GradientStop(0, new ColorF(1f, 1f, 0.2f, 1f)); GradientStopCollection radiantGradientStops = renderTarget.CreateGradientStopCollection(stops, Gamma.Linear, ExtendMode.Wrap); RadialGradientBrushProperties radialBrushProps = new RadialGradientBrushProperties(new Point2F(25, 25), new Point2F(0, 0), 10, 10); radialGradientBrush = renderTarget.CreateRadialGradientBrush(radialBrushProps, radiantGradientStops); } }
public Direct2DRenderer(IntPtr pWindowHandle) { Control control = Control.FromHandle(pWindowHandle); var renderProps = new RenderTargetProperties { PixelFormat = new PixelFormat( Microsoft.WindowsAPICodePack.DirectX.DXGI.Format.B8G8R8A8_UNORM, AlphaMode.Ignore), Usage = RenderTargetUsage.None, Type = RenderTargetType.Default // Software type is required to allow resource // sharing between hardware (HwndRenderTarget) // and software (WIC Bitmap render Target). }; _renderer = D2DFactory.CreateHwndRenderTarget( renderProps, new HwndRenderTargetProperties(pWindowHandle, new SizeU(Convert.ToUInt32(control.ClientSize.Width), Convert.ToUInt32(control.ClientSize.Height)), PresentOptions.Immediately)); }
/// <summary> /// This method creates the render target and all associated D2D and DWrite resources /// </summary> void CreateDeviceResources() { // Only calls if resources have not been initialize before if (renderTarget == null) { // Create the render target SizeU size = new SizeU((uint)host.ActualWidth, (uint)host.ActualHeight); RenderTargetProperties props = new RenderTargetProperties(); HwndRenderTargetProperties hwndProps = new HwndRenderTargetProperties(host.Handle, size, PresentOptions.None); renderTarget = d2dFactory.CreateHwndRenderTarget(props, hwndProps); // Create the black brush for text blackBrush = renderTarget.CreateSolidColorBrush(new ColorF(0, 0, 0, 1)); inlineImage = new ImageInlineObject(renderTarget, wicFactory, "TextInlineImage.img1.jpg"); TextRange textRange = new TextRange(14, 1); textLayout.SetInlineObject(inlineImage, textRange); } }
private void Situation_Load(object sender, EventArgs e) { Console.WriteLine("load!!"); // create factory (un-managed resource) d2DFactory = D2DFactory.CreateFactory(D2DFactoryType.SingleThreaded); dwriteFactory = DWriteFactory.CreateFactory(); // target RenderTargetProperties renderTargetProperties = new RenderTargetProperties { PixelFormat = new PixelFormat(), Usage = RenderTargetUsages.None, RenderTargetType = RenderTargetType.Default }; // handle HwndRenderTargetProperties hwndRenderTargetProperties1 = new HwndRenderTargetProperties { WindowHandle = this.pictureBox1.Handle, PixelSize = new SizeU((uint)this.pictureBox1.Width, (uint)this.pictureBox1.Height), PresentOptions = PresentOptions.Immediately }; renderTarget = d2DFactory.CreateHwndRenderTarget(renderTargetProperties, hwndRenderTargetProperties1); // create blushes blackBlush = renderTarget.CreateSolidColorBrush(new ColorF(0.0f, 0.0f, 0.0f)); redBlush = renderTarget.CreateSolidColorBrush(new ColorF(1.0f, 0.0f, 0.0f)); greenBlush = renderTarget.CreateSolidColorBrush(new ColorF(0.0f, 1.0f, 0.0f)); blueBlush = renderTarget.CreateSolidColorBrush(new ColorF(0.0f, 0.0f, 1.0f)); // define and start onPaintTimer onPaintTimer = new Timer(); onPaintTimer.Interval = 500; onPaintTimer.Tick += Render; onPaintTimer.Start(); }
private void SetRenderMode(RenderModes rm) { lock (renderSyncObject) { renderMode = rm; if (!IsInitialized && !isInitializing) { return; } //clean up objects that will be invalid after RenderTarget change if (dcRenderTarget != null) { dcRenderTarget.Dispose(); dcRenderTarget = null; } if (hwndRenderTarget != null) { hwndRenderTarget.Dispose(); hwndRenderTarget = null; } if (bitmapRenderTarget != null) { bitmapRenderTarget.Dispose(); bitmapRenderTarget = null; } peelings.Clear(); bitmap = null; //the bitmap created in dc render target can't be used in hwnd render target // Create the screen render target var size = new SizeU((uint)ClientSize.Width, (uint)ClientSize.Height); var props = new RenderTargetProperties { PixelFormat = new PixelFormat( Format.B8G8R8A8UNorm, AlphaMode.Ignore), Usage = RenderTargetUsages.GdiCompatible }; if (renderMode == RenderModes.DCRenderTarget || renderMode == RenderModes.BitmapRenderTargetOnPaint) { dcRenderTarget = d2DFactory.CreateDCRenderTarget(props); if (renderMode == RenderModes.BitmapRenderTargetOnPaint) { bitmapRenderTarget = dcRenderTarget.CreateCompatibleRenderTarget( CompatibleRenderTargetOptions.GdiCompatible, new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); } render = null; } else { hwndRenderTarget = d2DFactory.CreateHwndRenderTarget( props, new HwndRenderTargetProperties(Handle, size, Microsoft.WindowsAPICodePack.DirectX.Direct2D1.PresentOptions.RetainContents)); if (renderMode == RenderModes.BitmapRenderTargetRealTime) { bitmapRenderTarget = hwndRenderTarget.CreateCompatibleRenderTarget( CompatibleRenderTargetOptions.GdiCompatible, new Microsoft.WindowsAPICodePack.DirectX.Direct2D1.SizeF(ClientSize.Width, ClientSize.Height)); } render = RenderSceneInBackground; } //move all shapes to new rendertarget and refresh foreach (var shape in drawingShapes) { shape.Bitmap = Bitmap; shape.RenderTarget = RenderTarget; } RefreshAll(); } }