gpt4 book ai didi

c# - 如何使用 Direct2D 在透明背景上呈现 ClearType 文本?

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:44 24 4
gpt4 key购买 nike

谁能告诉我为什么 ClearType 在下面的简单 SharpDX 示例中拾取背景的透明度?以及如何阻止它这样做。

它将两行纯黑色文本呈现为半透明位图。第一行使用 Grayscale 渲染,第二行使用 ClearType 渲染。

编辑:有人提出问题是表面是预乘的。但这并不能解释两种 Grayscale/ClearType 渲染之间的差异。

结果(win7):

enter image description here

public static void Main()
{
var pixelFormat = SharpDX.WIC.PixelFormat.Format32bppPBGRA;

using (var wicFactory = new ImagingFactory())
using (var dddFactory = new SharpDX.Direct2D1.Factory())
using (var dwFactory = new SharpDX.DirectWrite.Factory())
using (var textFormat = new TextFormat(dwFactory, "Arial", FontWeight.Bold, FontStyle.Normal, 48))
using (var textLayout = new TextLayout(dwFactory, "argle-bargle", textFormat, float.PositiveInfinity, float.PositiveInfinity))
{
var width = (int) Math.Ceiling(textLayout.Metrics.Width);
var height = (int) Math.Ceiling(textLayout.Metrics.Height);

using (var wicBitmap = new SharpDX.WIC.Bitmap(
wicFactory,
width, height * 2,
pixelFormat,
BitmapCreateCacheOption.CacheOnLoad))
{
var renderTargetProperties = new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));
Color4 textColor = new Color4(0f, 0f, 0f, 1f);

using (var renderTarget = new WicRenderTarget(dddFactory, wicBitmap, renderTargetProperties))
using (var textBrush = new SolidColorBrush(renderTarget, textColor))
{
renderTarget.BeginDraw();
renderTarget.Clear(new Color4(1f, 1f, 0f, .25f));

renderTarget.TextAntialiasMode = TextAntialiasMode.Grayscale;
renderTarget.DrawTextLayout(new Vector2(0, 0), textLayout, textBrush);

renderTarget.TextAntialiasMode = TextAntialiasMode.Cleartype;
renderTarget.DrawTextLayout(new Vector2(0, height), textLayout, textBrush);

renderTarget.EndDraw();
}

var path = Path.Combine(Path.GetTempPath(), "test.png");
using (var stream = File.OpenWrite(path))
{
using (var encoder = new PngBitmapEncoder(wicFactory, stream))
using (var frameEncoder = new BitmapFrameEncode(encoder))
{
frameEncoder.Initialize();
frameEncoder.WriteSource(wicBitmap);
frameEncoder.Commit();
encoder.Commit();
}
}
}
}
}

最佳答案

这种意外结果是“正常”行为。

在 MSDN 中:

If you specify an alpha mode other than D2D1_ALPHA_MODE_IGNORE for a render target, the text antialiasing mode automatically changes from D2D1_TEXT_ANTIALIAS_MODE CLEARTYPE to D2D1_TEXT_ANTIALIAS_MODE GRAYSCALE.

因此,您第一次将 renderTarget.TextAntialiasMode 分配给 Grayscale 是没有意义的,因为 Grayscale 是其初始设置值。

MSDN 的另一段引述:

You can use the SetTextAntialiasMode method to change the text antialias mode back to D2D1_TEXT_ANTIALIAS_MODE CLEARTYPE, but rendering ClearType text to a transparent surface can create unpredictable results. If you want to render ClearType text to an transparent render target, we recommend that you use one of the following two techniques.

  • Use the PushAxisAlignedClip method to clip the render target to the area where the text will be rendered, then call the Clear method and specify an opaque color, then render your text.
  • Use DrawRectangle to draw an opaque rectangle behind the area where the text will be rendered.

这是完整的文章:D2D1_ALPHA_MODE enumeration

所以,如果我必须总结一下:

  1. 不要将 D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE 设置为具有不同于 D2D1_ALPHA_MODE_IGNORE 的 alpha 模式的渲染目标
  2. 遵循上述技巧。

关于c# - 如何使用 Direct2D 在透明背景上呈现 ClearType 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26308800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com