我需要一个字符串数组,例如:a[] = {A,B,C,...,Z}
然后从数组中随机选择一个字母,并在其上应用 Windows 目录中的字体,然后以具有特定宽度和高度的位图图像的形式将该特定字母呈现给用户(例如,将其显示为图像中的位图图像)我的表格中的框)。
最佳答案
嘿,如果我理解正确的话,你需要这样的东西:
//Create String-Array
string[] a = {"A", "B", "C"};
//Create a Image-Object on which we can paint
Image bmp = new Bitmap(100, 100);
//Create the Graphics-Object to paint on the Bitmap
Graphics g = Graphics.FromImage(bmp);
//Here we get the random string
//Random.Next() gives us the next integer value
//Because we dont want to get IndexOutOfBoundException we give the Array length to the Next method
//So just the numbers from 0 - Array.Length can be choosen from Next method
string randomString = a[new Random().Next(a.Length)];
//Your custom Font (6f = 6px)!
Font myFont = new Font("Arial", 6f)
//Get the perfect Image-Size so that Image-Size = String-Size
SizeF size = g.MeasureString(randomString, myFont);
PointF rect = new PointF(size.Width, size.Height);
//Use this to become better Text-Quality on Bitmap.
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
//Here we draw the string on the Bitmap
g.DrawString(randomString, myFont, new SolidBrush(Color.Black), rect);
您可以在程序中使用 bmp 对象。例如:
picturebox.Image = bmp;
我希望你现在能理解它:)如果你在理解对象设计时遇到问题,你应该首先阅读一本书。这是免费的;) http://openbook.galileocomputing.de/visual_csharp_2012/
请不要犹豫与我联系。 问候
关于c# - 字符转换为位图图像 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21307644/