To further refine the selection of fonts and avoid those that render text as squares, one strategy is to test a small sample string such as “Hello”. If the rendered text contains only standard alphanumeric characters, the font is likely acceptable. This test can be done by rendering the sample text and checking for unusual characters.
Here’s an enhanced script version with this approach:
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
class RandomImageGenerator
{
static void Main()
{
int width = 768;
int height = 768;
// Create a bitmap with the specified dimensions.
using (Bitmap bitmap = new Bitmap(width, height))
{
Random random = new Random();
// Generate random background color.
Color backgroundColor = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
// Fill the bitmap with the random background color.
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.Clear(backgroundColor);
// Generate random text.
string text = GenerateRandomText(random, 10); // You can adjust the length of the text here.
// Filter valid font families, excluding symbol-based fonts.
FontFamily[] allFontFamilies;
using (InstalledFontCollection fontCollection = new InstalledFontCollection())
{
allFontFamilies = fontCollection.Families;
}
FontFamily[] validFontFamilies = allFontFamilies
.Where(f => !f.Name.ToLower().Contains("ding") && !f.Name.ToLower().Contains("symbol"))
.Where(f => IsFontRenderable(f))
.ToArray();
if(validFontFamilies.Length == 0)
{
Console.WriteLine("No valid fonts found.");
return;
}
// Choose a random valid font.
FontFamily randomFontFamily = validFontFamilies[random.Next(validFontFamilies.Length)];
// Start with a large font size and decrease until text fits within the image.
Font font = null;
SizeF textSize = SizeF.Empty;
float fontSize = 100;
do
{
fontSize -= 1;
font = new Font(randomFontFamily, fontSize);
textSize = graphics.MeasureString(text, font);
}
while (textSize.Width > width || textSize.Height > height);
// Generate random text color.
Color textColor = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
// Calculate the position to draw the text so it's centered in the image.
float xPosition = (width - textSize.Width) / 2;
float yPosition = (height - textSize.Height) / 2;
// Draw the random text with random font and color.
using (Brush textBrush = new SolidBrush(textColor))
{
graphics.DrawString(text, font, textBrush, xPosition, yPosition);
}
// Save the bitmap to a file.
bitmap.Save("RandomImage.png");
}
}
Console.WriteLine("Random image generated and saved as 'RandomImage.png'.");
}
// Helper method to check if a font renders properly for standard alphanumeric characters.
static bool IsFontRenderable(FontFamily fontFamily)
{
using (Bitmap bmp = new Bitmap(1, 1))
using(Graphics g = Graphics.FromImage(bmp))
{
string sampleText = "Hello";
using (Font font = new Font(fontFamily, 20))
{
SizeF size = g.MeasureString(sampleText, font);
if (size.Width <= 0 || size.Height <= 0)
return false;
// Create a new Bitmap to draw the sample text and check its render
using (Bitmap testBmp = new Bitmap((int)size.Width, (int)size.Height))
using (Graphics testGraphics = Graphics.FromImage(testBmp))
{
testGraphics.Clear(Color.White);
testGraphics.DrawString(sampleText, font, Brushes.Black, new PointF(0, 0));
for (int x = 0; x < testBmp.Width; x++)
{
for (int y = 0; y < testBmp.Height; y++)
{
if (testBmp.GetPixel(x, y).ToArgb() != Color.Black.ToArgb() &&
testBmp.GetPixel(x, y).ToArgb() != Color.White.ToArgb())
{
return true;
}
}
}
}
}
}
return false;
}
// Helper method to generate a random string of the specified length.
static string GenerateRandomText(Random random, int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
char[] textChars = new char[length];
for (int i = 0; i < length; i++)
{
textChars[i] = chars[random.Next(chars.Length)];
}
return new string(textChars);
}
}
Changes
- IsFontRenderable Method: Added a method to test if a font renders standard alphanumeric text correctly.
- This function checks if drawing the sample text
"Hello" with the font using a small image results in proper rendering. It detects non-white and non-black pixels to determine if the text is rendered correctly, avoiding fonts that render as squares.
- Validation Integration: Integrated this validation in the filtering step to exclude fonts that don't pass the renderability test.
With this approach, you should get a list of fonts that are less likely to be symbol-based and that can render standard text correctly.