how to draw blank space in winform?

mc 7,231 Reputation points
2026-05-16T11:52:26.9066667+00:00

I am using winform and I want to draw blank space .

I create bitmap and Graphics from it and then draw strings.

the background is #000000 and the brush color is #ffffff and I draw texts is all right.

but I want draw only a blank string.

var text=" " and I want to there is a #ffffff blank in the background how to do it?

Developer technologies | Windows Forms
0 comments No comments

Answer accepted by question author

Nancy Vo (WICLOUD CORPORATION) 7,985 Reputation points Microsoft External Staff Moderator
2026-05-18T03:14:46.6766667+00:00

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:

  1. Using MeasureString and FillRectangle.

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);
    }
}
  1. Using TextRenderer.DrawText with 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.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AI answer

    2026-05-16T11:52:39.1533333+00:00

    Draw a filled rectangle (or other shape) instead of trying to draw a space character. A space has no visible glyph, so DrawString(" ", ...) will not paint anything.

    Example in a Paint handler using GDI+:

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // background is black (#000000)
    
        // draw a white (#ffffff) blank area  
        using (Brush brush = new SolidBrush(Color.White))
        {
            // choose the position and size of the blank area
            Rectangle rect = new Rectangle(50, 50, 100, 20);
            e.Graphics.FillRectangle(brush, rect);
        }
    
        // then draw text as usual
        using (Font font = new Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel))
        {
            PointF point = new PointF(50, 80);
            e.Graphics.DrawString("Hello", font, Brushes.White, point);
        }
    }
    

    If drawing onto a Bitmap, use the Graphics created from that bitmap in the same way:

    using (Graphics g = Graphics.FromImage(bitmap))
    using (Brush brush = new SolidBrush(Color.White))
    {
        g.FillRectangle(brush, new Rectangle(50, 50, 100, 20));
    }
    

    This paints a solid white region on the black background, effectively giving a “blank” white area.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.