ASP.NET中制作图形
2024-07-10 12:57:05
供稿:网友
这个程序经过修改 现在作计数器的话 只能做黑白的 计数器,谁有办法 能够做出 复杂的 图形计数器?
<% @page language="c#" %>
<% @import namespace="system.drawing" %>
<% @import namespace="system.io" %>
<% @import namespace="system.drawing.imaging" %>
<%
response.expires = 0;
bitmap newbitmap = null;
graphics g = null ;
string str2render = request.querystring.get("hitcount");
if (null == str2render) str2render = "12345";
string strfont = request.querystring.get("hitfontname");
if (null == strfont) strfont = "楷体_gb2312";
int nfontsize = 12;
try
{
nfontsize = request.querystring.get("hitfontsize").toint32();
}
catch
{
// do nothing, just ignore
}
string strbackgroundcolorname = request.querystring.get("hitbackgroundcolor");
color clrbackground = color.white;
try
{
if (null != strbackgroundcolorname)
clrbackground = colortranslator.fromhtml(strbackgroundcolorname);
}
catch
{
}
string strfontcolorname = request.querystring.get("hitfontcolor");
color clrfont = color.black;
try
{
// format in the url: %23xxxxxx
if (null != strfontcolorname)
clrfont = colortranslator.fromhtml(strfontcolorname);
}
catch
{
}
try
{
font fontcounter = new font(strfont, nfontsize);
newbitmap = new bitmap(1,1,pixelformat.format32bppargb);
g = graphics.fromimage(newbitmap);
sizef stringsize = g.measurestring(str2render, fontcounter);
int nwidth = (int)stringsize.width;
int nheight = (int)stringsize.height;
g.dispose();
newbitmap.dispose();
newbitmap = new bitmap(nwidth,nheight,pixelformat.format32bppargb);
g = graphics.fromimage(newbitmap);
g.fillrectangle(new solidbrush(clrbackground), new rectangle(0,0,nwidth,nheight));
g.drawstring(str2render, fontcounter, new solidbrush(clrfont), 0, 0);
memorystream tempstream = new memorystream();
newbitmap.save(tempstream,imageformat.gif);
response.clearcontent();
response.contenttype = "image/gif";
response.binarywrite(tempstream.toarray());
response.end();
}
catch (exception e)
{
response.write(e.tostring());
}
finally
{
if (null != g) g.dispose();
if (null != newbitmap) newbitmap.dispose();
}
%>