首页 新闻 会员 周边 捐助

文字生成图片

0
悬赏园豆:10 [已解决问题] 解决于 2012-04-25 22:44

想问下,如何将在文字可以生成图片,也可以选择背景呢? 最近想对这个功能研究一下,用在自己的博客网站的开发,谁有这方面的资料

C#
Mr丶Lee的主页 Mr丶Lee | 初学一级 | 园豆:10
提问于:2012-04-01 08:44
< >
分享
最佳答案
0

可以使用Graphics 实现

大概步骤如下

Graphics g=image. CreateGraphics()

g.drawstring("内容")

收获园豆:1
thtfria | 菜鸟二级 |园豆:203 | 2012-04-01 23:38
其他回答(3)
0

这个跟验证码类似的吧

收获园豆:1
artwl | 园豆:16736 (专家六级) | 2012-04-01 09:49
-1

参考代码:

// Create a new 32-bit bitmap image.
Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);

// Create a graphics object for drawing.
Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rect = new Rectangle(0, 0, this.width, this.height);

// Fill in the background.
HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
g.FillRectangle(hatchBrush, rect);

// Set up the text font.
SizeF size;
float fontSize = rect.Height + 1;
Font font;
// Adjust the font size until the text fits within the image.
do
{
fontSize--;
font = new Font(this.familyName, fontSize, FontStyle.Bold);
size = g.MeasureString(this.text, font);
} while (size.Width > rect.Width);

// Set up the text format.
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;

// Create a path using the text and warp it randomly.
GraphicsPath path = new GraphicsPath();
//text - 文字内容
path.AddString(text, font.FontFamily, (int) font.Style, font.Size, rect, format);
float v = 4F;
PointF[] points =
{
new PointF(this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
new PointF(this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v)
};
Matrix matrix = new Matrix();
matrix.Translate(0F, 0F);
path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);

// Draw the text.
hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.DarkGray, Color.DarkGray);
g.FillPath(hatchBrush, path);

// Clean up.
font.Dispose();
hatchBrush.Dispose();
g.Dispose();
收获园豆:5
dudu | 园豆:29568 (高人七级) | 2012-04-01 11:25
0

关注……

收获园豆:3
xiufang1989 | 园豆:280 (菜鸟二级) | 2012-04-01 16:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册