我有以下C#代码从Windows 7 64位上的位图获取图形
protected Graphics GetImageGraphics()
{
var g = Graphics.FromImage(image);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.FillRectangle(backBrush, new Rectangle(Point.Empty, Bounds.Size));
return g;
}
使用如下:
protected override void Redraw()
{
var g = GetImageGraphics();
var path = RoundedRectanglePath(shapeRect, CornerRadius);
g.FillPath(selected ? selectedBrush : brush, path);
g.DrawPath(pen, path);
foreach(var pin in pins)
pin.Value.Render(g);
var labelMax = shapeRect;
labelMax.Inflate(-10, -10);
var fmt = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak | TextFormatFlags.WordEllipsis;
TextRenderer.DrawText(g, Task.Symbol.Label, Font, labelMax, Color.Black, fmt);
fmt = TextFormatFlags.Left | TextFormatFlags.Top | TextFormatFlags.SingleLine | TextFormatFlags.WordEllipsis;
TextRenderer.DrawText(g, Task.TaskId, Font, idRect, Color.Black, fmt);
}
然后使用以下方法将创建的图像复制到屏幕上
g.DrawImageUnscaled(image, Bounds);
结果文字非常难看。它以纯黑色呈现,其中每个像素已被三个相邻的水平像素替换(如旧的点阵打印机粗体字体)
如果我将TextRenderingHint更改为AntiAlias或AntiAliasGridFit,则字体渲染得更清晰但没有任何抗锯齿(如SingleBitPerPixel)
我做错了什么?
答案 0 :(得分:0)
解决方案是双重的。正如汉斯指出的那样,文字不会变成透明的。答案的另一部分是不要在内存BitMaps上使用TextRenderer,如此处所述https://stackoverflow.com/a/1578056/429328