【发布时间】:2012-05-14 15:35:19
【问题描述】:
是否可以在图像上写 3D 文本。我正在使用带有 c# 的 asp.net。如果可能的话,请给我一些想法。
这是我在图像上写文字的方法
私有只读字符串 _textOnImage = ConfigurationManager.AppSettings["textOnImage"]; 私有只读 int _opacity = Int32.Parse(ConfigurationManager.AppSettings["opicity"]); 私有只读 int _red = Int32.Parse(ConfigurationManager.AppSettings["red"]); 私有只读 int _green = Int32.Parse(ConfigurationManager.AppSettings["green"]); 私有只读 int _blue = Int32.Parse(ConfigurationManager.AppSettings["blue"]); 私有 int _fontSize = Int32.Parse(ConfigurationManager.AppSettings["fontSize"]); 私有只读 String _fontName = ConfigurationManager.AppSettings["fontName"];
private bool _havException { get;放; } 私有字符串 _exceptionMessage { 获取;放; } 私有位图 SourceImage { 获取;放; } 私有位图 DestinationImage { 获取;放; } 公共图像方法() { _havException = 假; _exceptionMessage = string.Empty; }
public void AddWatermarkText(Image img, String TextOnImage)
{
TextOnImage = TextOnImage ?? _textOnImage;
try
{
var lobFromImage = Graphics.FromImage(img);
FindFontSize(lobFromImage, img, TextOnImage);
var lobFont = new Font(_fontName, _fontSize, FontStyle.Regular);
var lintTextHw = lobFromImage.MeasureString(TextOnImage, lobFont);
var lintTextOnImageWidth = (int)lintTextHw.Width;
var lintTextOnImageHeight = (int)lintTextHw.Height;
var lobSolidBrush = new SolidBrush(Color.FromArgb(_opacity, Color.FromArgb(_red, _green, _blue)));
var posLeft = (img.Width - lintTextOnImageWidth) / 2;
posLeft = posLeft > 0 ? posLeft : 5;
var lobPoint = new Point(posLeft, (img.Height / 2) - (lintTextOnImageHeight / 2));
// var lobPoint = new Point(RandomNumber(0, img.Width - lintTextOnImageWidth), RandomNumber(0, img.Height - lintTextOnImageHeight));
lobFromImage.DrawString(TextOnImage, lobFont, lobSolidBrush, lobPoint, StringFormat.GenericTypographic);
lobFromImage.Dispose();
lobSolidBrush.Dispose();
lobFont.Dispose();
}
catch (Exception ex)
{
_havException = true;
_exceptionMessage = ex.Message;
}
return;
}
private void FindFontSize(Graphics lobGraphics, Image lobImage ,String textOnImage )
{
var lobFont = new Font(_fontName, _fontSize, FontStyle.Regular);
var lintTextHw = lobGraphics.MeasureString(textOnImage, lobFont);
var lintTextOnImageWidth = (int)lintTextHw.Width;
var lintImageWidth = lobImage.Width ;
while (lintImageWidth < lintTextOnImageWidth)
{
lobFont = new Font(_fontName, _fontSize--, FontStyle.Regular);
lintTextOnImageWidth = (int)lobGraphics.MeasureString(textOnImage, lobFont).Width;
}
}
【问题讨论】:
-
我已经把我的代码放在图像上写简单的文本。如何将文本转换为 3D
-
对于“手动绘制”,我的意思是您实际上应该在其上绘制代表文本的图形。
DrawString方法不能做 3D 文本。