Introduction
Creates an image of evenly spaced numeric characters in ascending order following a grid pattern. I created this quick program while designing a game map that needed each box to be identified by a number. This program allowed me to quickly and easily change the numbers that were included, where they were located, and their font, all of which were constantly changing as the map was updated.
Background
Just involves a simple GUI using basic visual studio controls such as
ColorDialog
and FontDialog
. It takes the user's input to calculate the size of the final image document so that every number is evenly spaced on each row and column after taking into account the font style being used and measuring the number as a string.
Using the code
The only input necessary before hitting the "Create Image" button is to set how many columns & rows there are and their width/height in the NumericUpDown boxes.
Based on this input and the default values that can be changed for the background color, font color, and the font of the numbers to be drawn, the image will be created and shown to the user in a new form where the user can save the image using the file dropdown from the form's menu.
The bulk of where everything takes place is in the CreateImage
method, found in the Form1.cs file, which is called by hitting the CreateImageBtn
.
Bitmap CreateImage()
{
int totalNumbers = (int)(ColumnNbox.Value * RowNbox.Value);
int numCounter = 0;
int width = (int)(ColumnNbox.Value * ColumnWidthNbox.Value);
int height = (int)(RowNbox.Value * RowHeightNbox.Value);
Bitmap newImage = new Bitmap(width, height);
Graphics g = Graphics.FromImage(newImage);
SolidBrush numBrush = new SolidBrush(FontColorPbox.BackColor);
SizeF numSize = new SizeF();
PointF numPt = new PointF();
g.Clear(BackColorPbox.BackColor);
for (int r = 0; r < RowNbox.Value; r++)
{
for (int c = 0; c < ColumnNbox.Value; c++)
{
numCounter++;
numSize = g.MeasureString(numCounter.ToString(), characterFont);
numPt = new PointF((float)((c * ColumnWidthNbox.Value) + (ColumnWidthNbox.Value / 2)),
(float)((r * RowHeightNbox.Value) + (RowHeightNbox.Value / 2)));
g.DrawString(numCounter.ToString(), characterFont, numBrush,
new PointF(numPt.X - numSize.Width/2, numPt.Y - numSize.Height/2));
}
}
return newImage;
}
Points of Interest
I hadn't had to use the ColorDialog
before so I was surprised to find that it doesn't allow the user to set the alpha value of the color. Because of this, I ended up adding a
NumericUpDown
box used for setting the alpha value separately from selecting the base color from the
ColorDialog
.