位置:海鸟网 > IT > ASP.NET >

用ASP+制作图形(有了ASP+,不需要扩展图像组件啦!)

001  <% @Page Language="C#" %>002  <%
@Import Namespace="System.Drawing" %>003  <% @Import Namespace="System.IO" %>004  <% @Import Namespace="System.Drawing.Imaging" %
>005  <%006  Response.Expires = 0;007  Bitmap newBitmap = null;008  Graphics g = null ;009  010  string str2Render =
Request.QueryString.Get("HitCount");011  if (null == str2Render) str2Render = "no count
specified";012  string strFont = Request.QueryString.Get
("HitFontName");013  if (null ==
strFont) strFont = "Lucida Sans Unicode";014  015  int nFontSize =
12;016  try017  {018      nFontSize = Request.QueryString.Get("HitFontSize").ToInt32();019  }020  catch021  {022      // do nothing, just ignore023  }024  025  string strBackgroundColorname = Request.QueryString.Get("HitBackgroundColor");026  Color
clrBackground = Color.White;027  try028  {029      // Format in the URL: %23xxXXxx030  
    if (null != strBackgroundColorname)031      
    clrBackground = ColorTranslator.FromHTML(strBackgroundColorname);032  }033  catch034  {035  }036  037  string strFontColorName =
Request.QueryString.Get("HitFontColor");038  Color clrFont = Color.Black;039  try040  {041      // Format in the URL: %
23xxXXxx042      if (null != strFontColorName)043          clrFont = ColorTranslator.FromHTML
(strFontColorName);044  }045  catch046  {047  }048  049  try 050  {051      Font fontCounter = new Font(strFont, nFontSize);052  053      // calculate size of the string.054  
    newBitmap = new Bitmap(1,1,PixelFormat.Format32bppARGB);055      g = Graphics.FromImage(newBitmap);056      SizeF stringSize = g.MeasureString(str2Render,
fontCounter);057      int nWidth = (int)
stringSize.Width;058      int nHeight = (int)
stringSize.Height;059      g.Dispose();060      newBitmap.Dispose();061      062      newBitmap =
new Bitmap(nWidth,nHeight,PixelFormat.Format32bppARGB);063      g
= Graphics.FromImage(newBitmap);064      g.FillRectangle(new
SolidBrush(clrBackground), new Rectangle(0,0,nWidth,nHeight));065  066      g.DrawString
(str2Render, fontCounter, new SolidBrush(clrFont), 0, 0);067      
068      MemoryStream tempStream = new MemoryStream();069      newBitmap.Save(tempStream,ImageFormat.PNG);070      071      
Response.ClearContent();072      Response.ContentType = "image/png";073      Response.BinaryWrite
(tempStream.ToArray());074      
Response.End();075      // newBitmap.Save
(Response.OutputStream, ImageFormat.PNG);076      // newBitmap.Save("o:\TestApps\TestServer\test.png", ImageFormat.PNG) ;077  } 078  catch (Exception e)
079  {080  
    Response.Write(e.ToString());081  }082  finally 083  {084      if (null != g) g.Dispose();085      if (null != newBitmap) newBitmap.Dispose();086  }087  %>