c# - 字符转换为位图图像 C#

标签 c# arrays fonts bitmap character

我需要一个字符串数组,例如: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/

相关文章:

c++ - 在 C++ 的可变参数模板中获取可变参数 size_t... 参数的总和

c# - .Net Rest Web 服务响应具有默认的数据协定命名空间而不是预期的命名空间

javascript - Ajax响应: Map JSON to HTML

c# - Plugin.MediaManager.Forms:无法从Xamarin.Forms应用程序数据目录播放视频

javascript - 如何按三个不同的字段对 javascript 数组进行排序?

gd 中的 php 和真实类型集合

c# - 当页面视频全屏时将 Webview2 切换到全屏

C++ 动态,基于数组的堆栈

用于生成在悬停时改变颜色的多色文本的 jQuery 插件

php - 在 PHP 中列出可用字体