字符转换为位图图像 C#

Cow*_*Boy 2 c# arrays fonts character bitmap

例如,我需要一个字符串数组:a[] = {A,B,C,...,Z}然后从数组中随机选择一个字母并在其上应用 windows 目录中的字体,然后以具有特定宽度和高度的位图图像的形式向用户呈现该特定字母(例如在我的表单中的图像框中将其显示为位图图像)。

Seb*_*ebi 5

嘿,如果我理解你的权利,你需要这样的东西:

//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);
Run Code Online (Sandbox Code Playgroud)

您可以在程序中使用 bmp 对象。例如:

picturebox.Image = bmp;
Run Code Online (Sandbox Code Playgroud)

我希望你现在能理解它 :) 如果你在理解 Objectdesign 时遇到问题,你应该先读一本书。这是免费的;) http://openbook.galileocomputing.de/visual_csharp_2012/

不要犹豫,与我联系。问候