【发布时间】:2014-12-06 04:14:48
【问题描述】:
谁能告诉我为什么 ClearType 在下面的简单 SharpDX 示例中拾取背景的透明度?以及如何阻止它这样做。
它将两行纯黑色文本渲染为半透明位图。第一行用灰度渲染,第二行用 ClearType 渲染。
编辑:有人认为问题在于表面是预乘的。但这并不能解释两种 Grayscale/ClearType 渲染之间的区别。
结果(win7):
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.microsoft.com/en-us/library/windows/desktop/…)。你应该解释你真正的期望。
-
我希望灰度和 cleartype 渲染是一致的。如果无法创建非预乘曲面怎么办?
标签: c# direct2d sharpdx cleartype