字符转换为位图图像C# [英] Character convert to bitmap image C#

查看:174
本文介绍了字符转换为位图图像C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有例如字符串数组: A [] = {A,B,C,...,Z} ,然后随机选择一个字母从阵列,并从在其上的窗口目录应用字体然后渲染结合特定的宽度和高度,以用户位​​图图像的形式的特定的字母(例如显示为在我的形式在图像框的位图图像)。

I need to have an array of strings for example: a[] = {A,B,C,...,Z} and then randomly choose one letter from the array and apply a font from windows directory on it and then render that particular letter in the form of Bitmap image with specific width and heights to the user (for example display it as a bitmap image in the image box in my form).

推荐答案

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

Hey if i understand you right you need something like this:

//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对象。例如:

You can use the bmp object in your Program. For example:

picturebox.Image = bmp;

我希望你现在可以把它理解:)如果你有问题要了解Objectdesign,你应该先读一本书。这是免费的;) http://openbook.galileocomputing.de/visual_csharp_2012/

不要犹豫前来与我联系。
问候

Dont hesitate to come in touch with me. regards

这篇关于字符转换为位图图像C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆