新華網上的
下面使用GDI+及C#代碼
老規矩
下面是C#代碼
// Text
// Text
using System;
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
using System
namespace BrawDraw
{
public partial class Utility_Text
{
int _width =
public int Width
{
get
{
return _width;
}
set
{
_width = value;
}
}
int _height =
public int Height
{
get
{
return _height;
}
set
{
_height = value;
}
}
string _text = string
public string Text
{
get
{
return _text;
}
set
{
_text = value;
}
}
string _fontName =
public string FontName
{
get
{
return _fontName;
}
set
{
_fontName = value;
}
}
Color _fontColor = Color
public Color FontColor
{
get
{
return _fontColor;
}
set
{
_fontColor = value;
}
}
Color _backgroundColor = Color
public Color BackgroundColor
{
get
{
return _backgroundColor;
}
set
{
_backgroundColor = value;
}
}
//華文新魏
protected void Page_Load(object sender
{
if (Request
{
try
{
this
}
finally
{
}
}
if (Request
{
try
{
this
}
finally
{
}
}
if (Request
{
this
}
else if (Request
{
this
}
if (Request
{
this
}
else if (Request
{
this
}
if (this
{
this
}
string colorString =
if (Request
{
colorString = Request
}
else if (Request
{
colorString = Request
}
if (colorString
{
colorString =
}
this
string bgColorString =
if (Request
{
bgColorString = Request
}
else if (Request
{
bgColorString = Request
}
if (bgColorString
{
bgColorString =
}
this
if (!IsPostBack)
{
CreateImage(this
BackgroundColor);
}
}
//根據驗證字符串生成最終圖象
public void CreateImage(string text
fontColor
{
Bitmap theBitmap = new Bitmap(width
Graphics theGraphics = Graphics
theGraphics
theGraphics
theGraphics
theGraphics
// 背景
theGraphics
GraphicsPath gp = new GraphicsPath();
FontFamily fontFamily;
try
{
fontFamily = new FontFamily(fontName);
}
catch (Exception exc)
{
fontFamily = new FontFamily(
}
StringFormat format = new StringFormat();
format
format
gp
RectangleF rectF = gp
float scaleX = width *
float scaleY = height *
float offsetX = rectF
float offsetY = rectF
System
gp
Brush newBrush = new SolidBrush(fontColor);
theGraphics
//theGraphics
if (gp != null) gp
// 將生成的圖片發回客戶端
MemoryStream ms = new MemoryStream();
theBitmap
Response
Response
Response
theGraphics
theBitmap
Response
}
private Color ConvertColor(string colorString)
{
Array knownColors = System
foreach (object colorName in knownColors)
{
if (colorString
{
return Color
}
}
if (colorString
{
return ColorTranslator
}
try
{
int color
return Color
}
finally
{ }
return Color
}
}
}
調用方法
BrawDraw
(其中text後面傳入要顯示的文字
OK
如果你是使用靜態網頁
顯示效果圖
或許
正常的顯示應該是這樣
如果沒有此字體顯示出來的效果就是這樣
原因在於我在下面代碼中設置了沒有該字體時
這裡使用了一般帶中文操作系統的服務器上都會自動安裝的
try
{
fontFamily = new FontFamily(fontName);
}
catch (Exception exc)
{
fontFamily = new FontFamily(
}
還需要更完善?我想
From:http://tw.wingwit.com/Article/program/net/201311/12703.html