当前位置:编程学习 > C#/ASP.NET >>

【画图】 一图片上,如何加上文字,且该文字描上白边。。看图



一图片上,如何加上文字,且该文字描上白边。。看以上图或我的头像。
好心人帮忙下。 --------------------编程问答-------------------- 不可以先ps下,在搞吗? --------------------编程问答--------------------

图片可以是空白的背景,但文字是动态的,所以需要动态加上描边色。。 --------------------编程问答-------------------- 先用该文字向上,向左,向右,向下各10像素打印白色,然后再打印红色字即可. --------------------编程问答-------------------- 但是没有阴影. --------------------编程问答--------------------
因为文字是动态产生的,可能不定期都在改变
美工当然可以实现了,只是这样相对呆板些。

呵呵,看看程序能否实现。。。谢谢关注的朋友们。 --------------------编程问答-------------------- 图片上加文字:

//using System.Drawing;   
//using System.IO;   
//using System.Drawing.Imaging;   

private void AddTextToImg(string fileName,string text)   
{   
   if(!File.Exists(MapPath(fileName)))   
   {   
       throw new FileNotFoundException("The file don't exist!");   
   }   
      
   if( text == string.Empty )   
   {   
       return;   
   }   
   //还需要判断文件类型是否为图像类型   

   System.Drawing.Image image = System.Drawing.Image.FromFile(MapPath(fileName));   
   Bitmap bitmap = new Bitmap(image,image.Width,image.Height);   
   Graphics g = Graphics.FromImage(bitmap);   

   float fontSize = 12.0f;    //字体大小   
   float textWidth = text.Length*fontSize; //文本的长度   
   //下面定义一个矩形区域,以后在这个矩形里画上白底黑字   
   float rectX = 0;           
   float rectY = 0;   
   float rectWidth = text.Length*(fontSize+8);   
   float rectHeight = fontSize+8;   
   //声明矩形域   
   RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);   

   Font font = new Font("宋体",fontSize);   //定义字体   
   Brush whiteBrush = new SolidBrush(Color.White);   //白笔刷,画文字用   
   Brush blackBrush = new SolidBrush(Color.Black);   //黑笔刷,画背景用   

   g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight);      

   g.DrawString(text,font,whiteBrush,textArea);   
   MemoryStream ms = new MemoryStream( );   
   //保存为Jpg类型   
   bitmap.Save(ms,ImageFormat.Jpeg);   

   //输出处理后的图像,这里为了演示方便,我将图片显示在页面中了   
   Response.Clear();   
   Response.ContentType = "image/jpeg";   
   Response.BinaryWrite( ms.ToArray() );   

   g.Dispose();   
   bitmap.Dispose();   
   image.Dispose();   


--------------------编程问答--------------------
谢谢楼上的兄弟,可是没有阴暗呀?不算是描边。 --------------------编程问答--------------------  string stringText = " 全局、变换矩阵";
            FontFamily family = this.Font.FontFamily;
          
            int fontStyle = (int)FontStyle.Regular;
            int emSize = 60;
            Point origin = new Point(10, 10);
            StringFormat format = StringFormat.GenericTypographic;

            // Add the string to the path.
            myPath.AddString(stringText,
                family,
                fontStyle,
                emSize,
                origin,
                format);

            //Draw the path to the screen.
            e.Graphics.DrawPath(new Pen(Color.White, 10), myPath);
            e.Graphics.DrawPath(new Pen(Color.Blue, 5), myPath);

            



            myPath.Dispose(); --------------------编程问答-------------------- using System.Drawing;
using System.Drawing.Drawing2D;

 Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //g.Clear(Color.c);
            GraphicsPath myPath = new GraphicsPath();

            // Set up all the string parameters. 
            string stringText = "测试文字、.觉得对方";
            FontFamily family = new FontFamily("幼圆");
            int fontStyle = (int)FontStyle.Bold;
            int emSize = 120;
            Point origin = new Point(20, 20);
            StringFormat format = StringFormat.GenericDefault;

            Brush bru=new SolidBrush(Color.FromArgb(241,027,039));

            // Add the string to the path. 
            myPath.AddString(stringText,
                family,
                fontStyle,
                emSize,
                origin,
                format);

            //Draw the path to the screen. 
            e.Graphics.FillPath(bru, myPath);
            e.Graphics.DrawPath(new Pen(Color.White , 4), myPath);  --------------------编程问答--------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            string str = "超越";

            using (System.Drawing.Drawing2D.GraphicsPath myPath = new System.Drawing.Drawing2D.GraphicsPath())
            {
                using (SolidBrush b = new SolidBrush(Color.Red))
                {
                    using (Pen p = new Pen(Color.White, 4))
                    {
                        myPath.AddString(str, new FontFamily("幼圆"),(int) FontStyle.Bold, 100, new Point(10, 10), StringFormat.GenericDefault);
                        e.Graphics.Clear(Color.Black);
                        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        e.Graphics.FillPath(b, myPath);
                        e.Graphics.DrawPath(p, myPath);
                    }
                }
            } 

        }
    }
}




Public Class Form1

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim str As String = "超越"

        Using myPath As New System.Drawing.Drawing2D.GraphicsPath
            Using b As New SolidBrush(Color.Red), p As New Pen(Color.White, 4)
                myPath.AddString(str, New FontFamily("幼圆"), FontStyle.Bold, 100, New Point(10, 10), StringFormat.GenericDefault)
                e.Graphics.Clear(Color.Black)
                e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                e.Graphics.FillPath(b, myPath)
                e.Graphics.DrawPath(p, myPath)
            End Using
        End Using
    End Sub

End Class

--------------------编程问答-------------------- 我习惯的做法是8方向绘制,每个方向偏移一个像素,描边宽度取决于循环次数,一次循环像素偏移1.
至于投影,需要用到至少5方向(720角)的绘制,可以使用渐弱地色彩透明度来实现. --------------------编程问答-------------------- ..
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,