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.Text;
using AllEasy;
public partial class Title_photo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DB Db = new DB();
DataSet ds = ds = Db.Select("tb_News", "", "*", 1, 1, "NewsID", false, "", false);
//第一条新闻記錄
string l_str_title = Getlength(ds.Tables[0].Rows[0]["Title"].ToString(),10);
string _FontName = "方正超粗黑_GBK";
int _FontSize = 22;
string _ShowName = l_str_title;//要生成图片的文字;
Bitmap objBitmap = null;
Graphics g = null;
FontFamily ff = new FontFamily(_FontName);
Font stringFont = new Font(ff, _FontSize, FontStyle.Regular);
StringFormat stringFormat = new StringFormat();
stringFormat.FormatFlags = StringFormatFlags.NoWrap;
try
{
objBitmap = new Bitmap(1, 1);
g = Graphics.FromImage(objBitmap);
SizeF stringSize = g.MeasureString(_ShowName, stringFont);
int nWidth = (int)stringSize.Width;
int nHeight = (int)stringSize.Height;
g.Dispose();
objBitmap.Dispose();
objBitmap = new Bitmap(nWidth, nHeight);
g = Graphics.FromImage(objBitmap);
g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, nWidth, nHeight));
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString(_ShowName, stringFont, new SolidBrush(Color.Black), new PointF(0, 0), stringFormat);
objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
}
catch (Exception ee)
{
Response.Write(ee.ToString());
}
finally
{
if (null != g) g.Dispose();
if (null != objBitmap) objBitmap.Dispose();
Response.End();
}
}
}
/// <summary>
/// 截取字符,不帶省略號
/// </summary>
/// <param name="par">截取字符串</param>
/// <param name="le">要截取的長度</param>
/// <returns></returns>
public string Getlength(object par, int le)
{
string tem = Convert.ToString(par);
if (tem.Length > le)
{
return tem.Substring(0, le);
}
else
{
return tem;
}
}
}
這是在.net下生成图片的代码..参考下..