日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

c#中输入文本文字,将输入的文字生成图片

發布時間:2023/12/13 综合教程 22 生活家
生活随笔 收集整理的這篇文章主要介紹了 c#中输入文本文字,将输入的文字生成图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

private void button1_Click(object sender, EventArgs e)
{
//獲取文本
string text = this.txtName.Text;

//得到Bitmap(傳入Rectangle.Empty自動計算寬高)
Bitmap bmp = TextToBitmap(text, this.txtName.Font, Rectangle.Empty, this.txtName.ForeColor, this.txtName.BackColor);

//用PictureBox顯示
this.pictureBox1.Image = bmp;

//保存到桌面save.jpg
string directory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
bmp.Save(directory + "\save.jpg", ImageFormat.Jpeg);
}
//定義一個方法
/// <summary>
/// 把文字轉換才Bitmap
/// </summary>
/// <param name="text"></param>
/// <param name="font"></param>
/// <param name="rect">用于輸出的矩形,文字在這個矩形內顯示,為空時自動計算</param>
/// <param name="fontcolor">字體顏色</param>
/// <param name="backColor">背景顏色</param>
/// <returns></returns>
private Bitmap TextToBitmap(string text, Font font, Rectangle rect, Color fontcolor, Color backColor)
{
Graphics g;
Bitmap bmp;
StringFormat format = new StringFormat(StringFormatFlags.NoClip);
if (rect == Rectangle.Empty)
{
bmp = new Bitmap(1, 1);
g = Graphics.FromImage(bmp);
//計算繪制文字所需的區域大?。ǜ鶕挾扔嬎汩L度),重新創建矩形區域繪圖
SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);

int width = (int)(sizef.Width + 1);
int height = (int)(sizef.Height + 1);
rect = new Rectangle(0, 0, width, height);
bmp.Dispose();

bmp = new Bitmap(width, height);
}
else
{
bmp = new Bitmap(rect.Width, rect.Height);
}

g = Graphics.FromImage(bmp);

//使用ClearType字體功能
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.FillRectangle(new SolidBrush(backColor), rect);
g.DrawString(text, font, Brushes.Black, rect, format);
return bmp;
}

總結

以上是生活随笔為你收集整理的c#中输入文本文字,将输入的文字生成图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。