string _FontName = Request["fontname"].ToString();
int _FontSize = Convert.ToInt16(Request["fontsize"]);
string _ShowName = Request["str"].ToString();
Bitmap objBitmap = null;
Graphics g = null ;
Font stringFont = new Font(_FontName, _FontSize, FontStyle.Bold );
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.Yellow), 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();
}
此博客展示了ASP.NET代码,从请求中获取字体名称、大小和显示字符串,创建位图并在其上绘制字符串,保存为GIF格式输出。同时使用try-catch-finally结构处理可能出现的异常,确保资源正确释放。
2224

被折叠的 条评论
为什么被折叠?



