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  try
017  {
018   nFontSize = Int32.Parse(Request.QueryString.Get("HitFontSize"));
019  }
020  catch
021  {
022   // do nothing, just ignore
023  }
024  
025  string strBackgroundColorname = Request.QueryString.Get("HitBackgroundColor");
026  Color clrBackground = Color.White;
027  try
028  {
029   // Format in the URL: %23xxXXxx
030   if (null != strBackgroundColorname)
031   clrBackground = ColorTranslator.FromHtml(strBackgroundColorname);
032  }
033  catch
034  {
035  }
036  
037  string strFontColorName = Request.QueryString.Get("HitFontColor");
038  Color clrFont = Color.Black;
039  try
040  {
041   // Format in the URL: %23xxXXxx
042   if (null != strFontColorName)
043   clrFont = ColorTranslator.FromHtml(strFontColorName);
044  }
045  catch
046  {
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  %>