A set of .NET Framework managed libraries for developing graphical user interfaces.
Hello @mc ,
Thanks for your question.
Since a space " " is just an empty gap, DrawString won't paint any white pixels into it.
I recommend some workarounds:
- Using
MeasureStringandFillRectangle.
You can measure how big the space would be and then fill that area with White.
You can refer to following code example:
Bitmap bitmap = new Bitmap(400, 100);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.Clear(Color.Black);
Font font = new Font("Arial", 16);
string blankText = " ";
float x = 50;
float y = 20;
SizeF spaceSize = g.MeasureString(blankText, font);
using (SolidBrush whiteBrush = new SolidBrush(Color.White))
{
g.FillRectangle(whiteBrush, x, y, spaceSize.Width, spaceSize.Height);
}
}
- Using
TextRenderer.DrawTextwith background color.
This method lets you set both text color and background color directly.
Bitmap bitmap = new Bitmap(400, 100);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.Clear(Color.Black);
Font font = new Font("Arial", 16);
Point position = new Point(50, 20);
TextRenderer.DrawText(
g,
" ",
font,
position,
Color.White,
Color.White
);
}
I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.