图片上加文字://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();
}
//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();
}
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.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 SubEnd Class