在图像上书写3维文字

时间:2012-05-05 07:35:01

标签: asp.net c#-4.0


是否可以在图像上书写3D文字。我在使用带有c#的asp.net。如果可能的话,请给我一些想法。



这是我在图像上写文字的方法

private readonly string _textOnImage = ConfigurationManager.AppSettings["textOnImage"]; private readonly int _opacity = Int32.Parse(ConfigurationManager.AppSettings["opicity"]); private readonly int _red = Int32.Parse(ConfigurationManager.AppSettings["red"]); private readonly int _green = Int32.Parse(ConfigurationManager.AppSettings["green"]); private readonly int _blue = Int32.Parse(ConfigurationManager.AppSettings["blue"]); private int _fontSize = Int32.Parse(ConfigurationManager.AppSettings["fontSize"]); private readonly String _fontName = ConfigurationManager.AppSettings["fontName"];

private bool _havException { get; set; } private string _exceptionMessage { get; set; } private Bitmap SourceImage { get; set; } private Bitmap DestinationImage { get; set; } public ImageMethods() { _havException = false; _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; } }

1 个答案:

答案 0 :(得分:0)

是的,这是可能的,但您必须手动绘制。

请参阅this开始绘制图片。

接下来,探索System.Drawing命名空间,特别是具有绘制方法的Graphics类,并在上述帖子中使用。