如何使用轮廓在图像上绘制文本?

我一直在寻找和寻找图像轮廓的绘图文字?

在这里我的代码

private static void tulisnamafile(string imagepath, string textnya) { Image image = Image.FromStream(new MemoryStream(File.ReadAllBytes(imagepath))); Bitmap newImage = new Bitmap(640, 380); using (Graphics g = Graphics.FromImage(newImage)) { // Draw base image g.DrawImageUnscaled(image, 0, 0); //Static is HERE SolidBrush brushing = new SolidBrush(Color.White); Font font = new Font(("Comic Sans MS"), 20.0f); int napoint = newImage.Height - 90; int napointa = image.Width - 200; FontFamily ff = new FontFamily("Times New Roman"); int fontSize = 24; Font f = new Font(ff, fontSize, FontStyle.Regular); StringFormat sf = new StringFormat(); Rectangle displayRectangle = new Rectangle(new Point(5, napoint), new Size(newImage.Width - 1, newImage.Height - 1)); g.DrawEllipse(Pens.Magenta, new Rectangle(0, 0, 1, 1)); GraphicsPath gp = new GraphicsPath(); gp.AddString(textnya, ff, (int)FontStyle.Bold, fontSize + 4, new Point(0, 0), sf); g.FillPath(Brushes.White, gp); g.DrawPath(Pens.Black, gp); g.Flush(FlushIntention.Sync); g.Dispose(); } image.Dispose(); string fileName = "ab.jpg"; string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); MessageBox.Show(path); newImage.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); newImage.Dispose(); } 

它的触发器

  private void button3_Click(object sender, EventArgs e) { string imagename = "C:\\Documents and Settings\\admin\\My Documents\\Visual Studio 2008\\Projects\\template\\template\\bin\\Debug\\bg.jpg"; tulisnamafile(imagename, "SlimPort® SP1002; Connect mobile devices to any big screen. High Speed micro USB"); } 

检查代码结果:

混乱的结果 这样的混乱结果,UNWRAPPED和白色

这就是我想要的东西,还有包装?

这个

我在CodeProject中发现但运气不好,它使用的是C ++。 以neowin为基础的人也试过这个…

但还是没有运气。

更新:

在这里我的工作代码,对于谁可能需要它…基于Abdias Software的代码(检查答案),我做了一些小改动(这些代码中有一些错误)。

  private static void tulisnamafile(string imagepath, string textnya) { float fontSize = 22; Image image = Image.FromStream(new MemoryStream(File.ReadAllBytes(imagepath))); //some test image for this demo Bitmap bmp = (Bitmap)Image.FromFile(imagepath); Graphics g = Graphics.FromImage(bmp); //this will center align our text at the bottom of the image StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Far; //define a font to use. Font f = new Font("Impact", fontSize, FontStyle.Bold, GraphicsUnit.Pixel); //pen for outline - set width parameter Pen p = new Pen(ColorTranslator.FromHtml("#77090C"), 8); p.LineJoin = LineJoin.Round; //prevent "spikes" at the path //this makes the gradient repeat for each text line Rectangle fr = new Rectangle(0, bmp.Height - f.Height, bmp.Width, f.Height); LinearGradientBrush b = new LinearGradientBrush(fr, ColorTranslator.FromHtml("#FF6493"), ColorTranslator.FromHtml("#D00F14"), 90); //this will be the rectangle used to draw and auto-wrap the text. //basically = image size Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height); GraphicsPath gp = new GraphicsPath(); //look mom! no pre-wrapping! gp.AddString(textnya, f.FontFamily, (int)FontStyle.Bold, fontSize, r, sf); //these affect lines such as those in paths. Textrenderhint doesn't affect //text in a path as it is converted to ..well, a path. g.SmoothingMode = SmoothingMode.AntiAlias; g.PixelOffsetMode = PixelOffsetMode.HighQuality; //TODO: shadow -> g.translate, fillpath once, remove translate g.DrawPath(p, gp); g.FillPath(b, gp); //cleanup gp.Dispose(); b.Dispose(); b.Dispose(); f.Dispose(); sf.Dispose(); g.Dispose(); string fileName = "ab.jpg"; string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); bmp.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); bmp.Dispose(); } 

总结一下:

定义一个GraphicPath ,然后使用DrawPath绘制文本的轮廓版本,使用DrawPath绘制填充版本。

对于与女性的第二个图像,第一个(填充的)版本首先以较小的偏移量绘制。

对于渐变,使用LinearGradientBrush作为brush 。 轮廓的厚度由笔的厚度定义。

用于包装定义StringFormat并使用Rectangle定义您希望文本所在的区域。

要使文本居中,可以将矩形定义为与图像具有相同的宽度,然后将strformat.Alignment设置为Center

更新:要复制第二个图像中的文本,您可以使用此代码:

 float fontSize = 52; //some test image for this demo Bitmap bmp = (Bitmap)Image.FromFile(s"test.jpg"); Graphics g = Graphics.FromImage(bmp); //this will center align our text at the bottom of the image StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Far; //define a font to use. Font f = new Font("Impact", fontSize, FontStyle.Bold, GraphicsUnit.Pixel); //pen for outline - set width parameter Pen p = new Pen(ColorTranslator.FromHtml("#77090C"), 8); p.LineJoin = LineJoin.Round; //prevent "spikes" at the path //this makes the gradient repeat for each text line Rectangle fr = new Rectangle(0, bmp.Height - f.Height, bmp.Width, f.Height); LinearGradientBrush b = new LinearGradientBrush(fr, ColorTranslator.FromHtml("#FF6493"), ColorTranslator.FromHtml("#D00F14"), 90); //this will be the rectangle used to draw and auto-wrap the text. //basically = image size Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height); GraphicsPath gp = new GraphicsPath(); //look mom! no pre-wrapping! gp.AddString("Demo for Stack Overflow", f.FontFamily, (int)f.Style, fontSize, r, sf); //these affect lines such as those in paths. Textrenderhint doesn't affect //text in a path as it is converted to ..well, a path. g.SmoothingMode = SmoothingMode.AntiAlias; g.PixelOffsetMode = PixelOffsetMode.HighQuality; //TODO: shadow -> g.translate, fillpath once, remove translate g.DrawPath(p, gp); g.FillPath(b, gp); //cleanup gp.Dispose(); b.Dispose(); b.Dispose(); f.Dispose(); sf.Dispose(); g.Dispose(); bmp.Save(s"test_result.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); bmp.Dispose(); 

这将产生以下结果:

得到的图像

要产生额外的“阴影”,只需首先翻译g ,绘制填充,然后删除翻译。

这里使用的是FromHtml ,因为我从你的图像中选择颜色并且懒得转换。 只需使用Color.FromARGB()或固定颜色 – 如您所愿。

VB版:

  Dim fontSize As Single = 52 Dim bmp As Bitmap = Bitmap.FromFile("c:\test.jpg") Dim g As Graphics = Graphics.FromImage(bmp) Dim sf As New StringFormat(StringFormatFlags.NoClip) sf.Alignment = StringAlignment.Center sf.LineAlignment = StringAlignment.Far Dim f As New Font("Impact", fontSize, FontStyle.Bold, GraphicsUnit.Pixel) Dim p As New Pen(ColorTranslator.FromHtml("#77090C"), 4) p.LineJoin = LineJoin.Round 'rectangle for font to repeat gradient for each line Dim fr As New Rectangle(0, bmp.Height - f.Height, bmp.Width, f.Height) Dim b As New LinearGradientBrush(fr, ColorTranslator.FromHtml("#FF6493"), ColorTranslator.FromHtml("#D00F14"), 90) Dim r As New Rectangle(0, 0, bmp.Width, bmp.Height) Dim gp As New GraphicsPath gp.AddString("Demo for Stack Overflow", f.FontFamily, f.Style, fontSize, r, sf) g.SmoothingMode = SmoothingMode.AntiAlias g.PixelOffsetMode = PixelOffsetMode.HighQuality g.DrawPath(p, gp) g.FillPath(b, gp) gp.Dispose() 'path b.Dispose() 'b b.Dispose() 'p f.Dispose() 'font sf.Dispose() 'stringformat g.Dispose() 'g bmp.Save("c:\test_result.jpg", Imaging.ImageFormat.Jpeg) bmp.Dispose() 

获得“更好”结果的简单方法可能是两次绘制文本。 首先绘制阴影,例如,如果您想要灰色的经典阴影外观,则向右和向下绘制一些像素。 您可能还想考虑使用不同的字体,任何没有衬线的字体看起来会更好我猜。 有关渐变效果,请参阅msdn页面或google如何使用它。 此外,使用图形对象的SmoothingMode和TextRenderingHint ,HighQuality和Antialias应该会产生更好看的效果。