可以使用 DirectWrite 将文本渲染到 WinForm 应用程序中的 PictureBox 吗?
我正在使用 SharpDX 并且已经通过 DirectWrite 示例尝试构建我可以构建的最简单的工作案例。
我创建了一个表单并只添加了一个图片框。然后是下面的代码。窗体显示,但在 PictureBox 中看不到任何内容。
任何指导将不胜感激。
谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
//using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharpDX.Direct2D1;
using SharpDX.DXGI;
using SharpDX;
using SharpDX.DirectWrite;
using AlphaMode = SharpDX.Direct2D1.AlphaMode;
using Factory = SharpDX.Direct2D1.Factory;
namespace d2dwTextEdit
{
public partial class Form1 : Form
{
public Factory Factory2D { get; private set; }
public SharpDX.DirectWrite.Factory FactoryDWrite { get; private set; }
public WindowRenderTarget RenderTarget2D { get; private set; }
public SolidColorBrush SceneColorBrush { get; private set; }
public TextFormat TextFormat { get; private set; }
public SharpDX.RectangleF ClientRectangle { get; private set; }
public Form1()
{
InitializeComponent();
Initialize();
Render();
}
protected void Initialize()
{
Factory2D = new SharpDX.Direct2D1.Factory();
FactoryDWrite = new SharpDX.DirectWrite.Factory();
HwndRenderTargetProperties properties = new HwndRenderTargetProperties();
properties.Hwnd = pictureBox1.Handle;
properties.PixelSize = new System.Drawing.Size(pictureBox1.Width, pictureBox1.Height);
properties.PresentOptions = PresentOptions.None;
TextFormat = new TextFormat(FactoryDWrite, "Calibri", 30) { TextAlignment = TextAlignment.Center, ParagraphAlignment = ParagraphAlignment.Center };
RenderTarget2D = new WindowRenderTarget(Factory2D, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)), properties);
RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;
RenderTarget2D.TextAntialiasMode = TextAntialiasMode.Cleartype;
ClientRectangle = new RectangleF(0, 0, pictureBox1.Width, pictureBox1.Height);
SceneColorBrush = new SolidColorBrush(RenderTarget2D, Colors.White);
SceneColorBrush.Color = Colors.Black;
}
private void Render()
{
RenderTarget2D.Clear(Colors.White);
RenderTarget2D.DrawText("Hello Marshall", TextFormat, ClientRectangle, SceneColorBrush);
}
}
}