嘿,如果我理解你的权利,你需要这样的东西:
//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/
不要犹豫,与我联系。问候