C#中使用GDI+让网站新闻标题个性化
有时,我们需要让新闻标题更加个性化,如下图:
上面这幅图片是新华网上的今日头条的标题。
我们一般的做法可能是:使用Photoshop制作成图片,保存上传。这样需要浪费人力,比较麻烦。有没有更好的办法呢?
下面使用GDI+及C#代码,完成自动实现的过程。
老规矩,先看看运行效果:
下面是C#代码:
// Text2Image.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Text2Image.aspx.cs" Inherits="BrawDraw.Com.Utility.Utility_Text2Image" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>生成图片验证码</title>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
// Text2Image.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
namespace BrawDraw.Com.Utility
{
public partial class Utility_Text2Image : System.Web.UI.Page
{
int _width = 480;
public int Width
{
get
{
return _width;
}
set
{
_width = value;
}
}
int _height = 66;
public int Height
{
get
{
return _height;
}
set
{
_height = value;
}
}
string _text = string.Empty;
public string Text
{
get
{
return _text;
}
set
{
_text = value;
}
}
string _fontName = "宋体";
public string FontName
{
 
推荐文章 |
