Tile a bitmap with a string in C#

[bitmap]

I recently needed a bitmap tiled with a string for a book I’m working on, so I wrote this program to make it. The program lets you click on the font name, dimensions, or colors to select those values. When you click on one of those items, the program display an appropriate dialog. For example, if you click on the background color swatch (the swatch on the left), the program executes the following code.

private void picBackground_Click(object sender, EventArgs e)
{
    cdColor.Color = picBackground.BackColor;
    if (cdColor.ShowDialog() == DialogResult.OK)
        picBackground.BackColor = cdColor.Color;
}

This code sets the Color property for the ColorDialog component named cdColor to the color in the picBackground PictureBox control. It displays the dialog and, if you pick a color and click OK, the code updates the PictureBox control’s BackColor property to show the newly selected color.

The other pieces of value selection code are similar. Download the example to see how they work.

When you click the Go button, the following code generates and saves the bitmap.

private void btnGo_Click(object sender, EventArgs e)
{
    DrawText();
}

private void DrawText()
{
    int width = int.Parse(txtWidth.Text);
    int height = int.Parse(txtHeight.Text);

    Bitmap bm = new Bitmap(width, height);
    using (Graphics gr = Graphics.FromImage(bm))
    {
        gr.Clear(picBackground.BackColor);

        using (Brush brush =
            new SolidBrush(picForeground.BackColor))
        {
            string text = txtString.Text;
            SizeF size = gr.MeasureString(text, fdFont.Font);
            for (float y = 0; y < bm.Height;
                y += size.Height * 0.8f)
            {
                float x = y;
                while (x > 0) x -= size.Width;

                while (x <= bm.Width)
                {
                    gr.DrawString(text, fdFont.Font, brush, x, y);
                    x += size.Width;
                }
            }
        }

        bm.Save("Result.png", ImageFormat.Png);
        picImage.Image = bm;
    }
}

The button's Click event handler simply invokes the DrawText method. That method gets the entered width and height. It uses them to create a bitmap of the correct size and creates an associated Graphics object.

The code then clears the bitmap with the color stored in the picBackground control's BackColor property. It then creates a brush that uses the color stored in the picForeground control's BackColor property.

The code gets the desired text and calculates its size. It then enters a loop to draw each line of text. For each line, the program sets x = y to move the new line to the right by the same distance it has been moved down. The code subtracts the string's width until x is at most zero, so the line starts to the left of the visible area.

Next, the code enters a loop that prints the string and moves x to the right by the string's width until the string has reached the right edge of the bitmap.

After it finishes filling the bitmap with text, the program saves the result in a PNG file and displays the bitmap in the picImage PictureBox control.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in algorithms, graphics, image processing and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.