I am writing C# code in VS2010 to achieve the following.
I am trying to draw text on a transparent bitmap and running into ugly looking text. Below is the pic of three texts (i.e. A, B and C)
Note: A and C have transparent background, B has White as background.
Problem:
A: I have a bitmap with ARGB pixel format. And I want to draw a text into it. The text drawn has sharp edges and it does not honor the transparency.
B: I write the exact same code, but only difference from A is that the pixels in bitmap are not transparent anymore. (I will fill in a rectangle). But if you notince the pixels of the text, you will see a smooth blend of black (text color) and white (background color).
C: This is what I am expecting the text to look when I am drawing a text onto a bitmap with transparent pixels.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
//e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0, Color.Red)), e.ClipRectangle);
e.Graphics.DrawString("0123456789", new Font("Verdana", 20), Brushes.Red, new PointF(5, 10));
e.Graphics.DrawEllipse(Pens.Black, e.ClipRectangle);
}
// This is a public method, using which I want to save my usercontrol to an image to save to file later.
public Image ToImage(Size size, PixelFormat p)
{
Bitmap b = new Bitmap(Width, Height, p);
using (Graphics g = Graphics.FromImage(b))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
PaintEventArgs e = new PaintEventArgs(g, new Rectangle(0, 0, size.Width, size.Height));
base.OnPaint(e);
this.OnPaint(e);
}
return b;
}
How do I make A look like C with the transparent background. I am pretty sure this can be done, because softwares like photoshop, paint.net handle this very nicely.
Thanks a lot!
Datte