Antialiased text on transparent bitmap

后端 未结 2 695
执笔经年
执笔经年 2020-12-03 18:36

I\'d like to draw antialiased text on a transparent bitmap and have the antialiasing drawn as alpha blended pixels. This way, I can draw the bitmap onto any color surface (o

2条回答
  •  生来不讨喜
    2020-12-03 18:57

    I'm not sure if it's really necessary, but if you want to do alpha-blending, you should specify a 32-bit image:

    Bitmap bitmap = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    

    Using your example, I was able to make the text look decent by adding a text rendering hint:

    g.Clear(Color.Empty);
    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    g.DrawString("hello world", new Font(this.Font.FontFamily, 24), Brushes.Blue, new Point(50, 50));
    

    Is this doing what you want, or just hiding the problem?

提交回复
热议问题