在WPF中关闭图形到位图的抗锯齿
我使用WPF将文本渲染为位图。我想关闭抗锯齿,我的意思是我希望像素是白色或黑色。但是文本仍然模糊,有些像素甚至是灰色的 这是我的密码。有些线路可能不需要在WPF中关闭图形到位图的抗锯齿,wpf,drawing,antialiasing,Wpf,Drawing,Antialiasing,我使用WPF将文本渲染为位图。我想关闭抗锯齿,我的意思是我希望像素是白色或黑色。但是文本仍然模糊,有些像素甚至是灰色的 这是我的密码。有些线路可能不需要 RenderTargetBitmap bm = new RenderTargetBitmap(bitmapWidth, bitmapHeight, dpiX, dpiY, PixelFormats.Pbgra32); DrawingVisual drawing_visual = new DrawingVisual(); RenderOpti
RenderTargetBitmap bm = new RenderTargetBitmap(bitmapWidth, bitmapHeight, dpiX, dpiY, PixelFormats.Pbgra32);
DrawingVisual drawing_visual = new DrawingVisual();
RenderOptions.SetEdgeMode(drawing_visual, EdgeMode.Unspecified);
RenderOptions.SetBitmapScalingMode(drawing_visual, BitmapScalingMode.Linear);
RenderOptions.SetEdgeMode(bm, EdgeMode.Unspecified);
RenderOptions.SetBitmapScalingMode(bm, BitmapScalingMode.Linear);
DrawingContext drawing_context = drawing_visual.RenderOpen();
FormattedText ft = new FormattedText("my text", CultureInfo.InvariantCulture, System.Windows.FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black);
drawing_context.DrawText(ft, new Point(0, 0));
drawing_context.Close();
bm.Render(drawing_visual);
渲染图像:
要检查解决方案,可以从GitHub下载源代码:
https://github.com/qub1n/Font-rendering.git
这里的重要概念是。这是合乎逻辑的,因为您不需要抗锯齿文本,所以必须将TextRenderingMode设置为。困难在于把它放在哪里 我建议您创建一个DrawingImage作为开始对象,如下所示(将其用作画布源,而不是位图): 请注意设置为的同样重要的值。如果不这样做并保留默认值(),则仍将进行抗锯齿处理 现在,为了在显示器上进行渲染,您必须在可视元素上使用TextRenderingMode,因此在您的示例中,在画布元素上:
<controls:ImageCanvas ... TextOptions.TextRenderingMode="Aliased" ... />
事实证明,在
DrawInvisional
上设置TextOptions.SetTextRenderingMode()
不起作用。已接受的答案在生成的位图中仍然存在别名问题(最好使用textformatingmode.Display
,但仍然不是完全没有关联)
对我有用的是在图形视觉上设置内部属性:
var visual = new DrawingVisual();
var property = typeof(Visual).GetProperty("VisualTextRenderingMode",
BindingFlags.NonPublic | BindingFlags.Instance);
property.SetValue(visual, TextRenderingMode.Aliased);
文本应使用WPF进行特殊处理。也许你应该试试TextOptions.TextFormattingMode和/或TextRenderingMode:TextFormattingMode没有解决这个问题,TextRenderingMode不包含没有抗锯齿的设置。你对屏幕可视化或位图(类似.png的文件)创建感兴趣吗?它们非常不同。特别是在位图中,我想导出它。将
EdgeMode
设置为别名如何?
public static void SaveToFile(string filePath, DrawingImage drawing)
{
var image = new Image { Source = drawing };
image.Arrange(new Rect(new Size(drawing.Width, drawing.Height)));
// this is the important line here!
TextOptions.SetTextRenderingMode(image, TextRenderingMode.Aliased);
// note if you use a different DPI than the screen (96 here), you still will have anti-aliasing
// as the system is pretty automatic anyway
var bitmap = new RenderTargetBitmap((int)drawing.Width, (int)drawing.Height, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(image);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.Save(fileStream);
}
}
var visual = new DrawingVisual();
var property = typeof(Visual).GetProperty("VisualTextRenderingMode",
BindingFlags.NonPublic | BindingFlags.Instance);
property.SetValue(visual, TextRenderingMode.Aliased);