在很多时候,我们都要绘制纯色的图片,而用Graphics生成的往往是不纯的,尤其是绘制文字时。比如说绘制纯红色文字,往往R达不到255。
C#中默认抗锯齿,给人看起来柔和;但是我们现实中往往用到锯齿。
C#中可以通过下面的方法解决
Image bit = new Bitmap(400, 400);
Graphics g = Graphics.FromImage(bit);
//加上这句
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
g.Clear(pictureBox1.BackColor);
g.DrawString("黄色", new Font("宋体", 24), Brushes.Yellow, 0, 0);
g.DrawString("红色", new Font("宋体", 24), Brushes.Red, 0, 32);
wpf中用DrawingVisual绘图消除颜色不纯方法
- 显示非文本锯齿
public class AliasedDrawingVisual : DrawingVisual
{
public AliasedDrawingVisual()
{
this.VisualEdgeMode = EdgeMode.Aliased;
}
}继承DrawingVisual,修改VisualEdgeMode = EdgeMode.Aliased
- 显示文本锯齿
AliasedDrawingVisual drawingVisual = new AliasedDrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
FormattedText formattedText = new FormattedText(
"测试文本锯齿",
CultureInfo.GetCultureInfo("zh-cn"),
FlowDirection.LeftToRight, new Typeface("SimSun"),
24,
new SolidColorBrush(Color.FromRgb(255, 0, 0)),null,TextFormattingMode.Display);//TextFormattingMode.Display消除文本锯齿
drawingContext.DrawText(formattedText, new Point((544 - 8 * 24) / 2, 0));
drawingContext.DrawRectangle(null, new Pen(Brushes.Red, 1), new Rect(4, 52, 134 * 2, 19));实例化FormattedText时,传入TextFormattingMode.Display即可显示锯齿