如何在 C# 中使用自定义字体将文本转换为图像
How to convert Text into Image with Custom font in c#
我正在尝试将文本保存为带有字体 Cookie 的图像,但是当我转换带有字体的文本时,我不会根据需要影响那里。请查看代码并提出可能的解决方案。
提前致谢
string text = "Signature sting to convert";
Bitmap bitmap = new Bitmap(1, 1);
FontFamily myFontFamily = new FontFamily("cookie");
Font font = new Font(myFontFamily, 25, FontStyle.Regular, GraphicsUnit.Pixel);
Graphics graphics = Graphics.FromImage(bitmap);
int width = (int)graphics.MeasureString(text, font).Width;
int height = (int)graphics.MeasureString(text, font).Height;
bitmap = new Bitmap(bitmap, new Size(width, height));
graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.DrawString(text, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0);
graphics.Flush();
graphics.Dispose();
string fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".jpg";
bitmap.Save(Server.MapPath("~/images/") + fileName, ImageFormat.Jpeg);
imgText.ImageUrl = "~/images/" + fileName;
imgText.Visible = true;
我从以下行得到的异常
FontFamily myFontFamily = new FontFamily("cookie");
Font 'cookie' cannot be found.
请建议在这种情况下如何使用该字体。
我假设你需要字体,下载它:
http://www.1001freefonts.com/cookie.font,
双击 cookie.zip 文件,然后拖动文件:
Cookie-Regular.ttf
然后放在桌面上。
双击它,然后 (windows-7) 查看器将显示字体。
最上面是安装按钮,点击它。
查看器会将字体安装到 windows 字体文件夹。
我在Visual studio 2015年使用windows 7进行C#编程,安装字体后保存了位图。
我正在尝试将文本保存为带有字体 Cookie 的图像,但是当我转换带有字体的文本时,我不会根据需要影响那里。请查看代码并提出可能的解决方案。 提前致谢
string text = "Signature sting to convert";
Bitmap bitmap = new Bitmap(1, 1);
FontFamily myFontFamily = new FontFamily("cookie");
Font font = new Font(myFontFamily, 25, FontStyle.Regular, GraphicsUnit.Pixel);
Graphics graphics = Graphics.FromImage(bitmap);
int width = (int)graphics.MeasureString(text, font).Width;
int height = (int)graphics.MeasureString(text, font).Height;
bitmap = new Bitmap(bitmap, new Size(width, height));
graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.White);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
graphics.DrawString(text, font, new SolidBrush(Color.FromArgb(255, 0, 0)), 0, 0);
graphics.Flush();
graphics.Dispose();
string fileName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".jpg";
bitmap.Save(Server.MapPath("~/images/") + fileName, ImageFormat.Jpeg);
imgText.ImageUrl = "~/images/" + fileName;
imgText.Visible = true;
我从以下行得到的异常
FontFamily myFontFamily = new FontFamily("cookie");
Font 'cookie' cannot be found.
请建议在这种情况下如何使用该字体。
我假设你需要字体,下载它:
http://www.1001freefonts.com/cookie.font,
双击 cookie.zip 文件,然后拖动文件:
Cookie-Regular.ttf
然后放在桌面上。
双击它,然后 (windows-7) 查看器将显示字体。
最上面是安装按钮,点击它。
查看器会将字体安装到 windows 字体文件夹。
我在Visual studio 2015年使用windows 7进行C#编程,安装字体后保存了位图。