100分求解,如何把生成的随机数生成图片输出在网页上?解决立即结贴!

zbr4777 2003-08-20 10:28:16
100分求解,如何把生成的随机数生成图片输出在网页上?解决立即结贴!
...全文
68 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
acewang 2003-08-20
  • 打赏
  • 举报
回复
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

public class CTestBitmapFunctionality
{
public static void Main()
{
Bitmap newBitmap = null;
Graphics g = null ;

try
{
Font fontCounter = new Font("Lucida Sans Unicode", 12);

// calculate size of the string.
newBitmap = new Bitmap(1,1,PixelFormat.Format32bppARGB);
g = Graphics.FromImage(newBitmap);
SizeF stringSize = g.MeasureString("Hello World", 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(Color.White),
new Rectangle(0,0,nWidth,nHeight));

g.DrawString("Hello World", fontCounter,
new SolidBrush(Color.Black), 0, 0);

newBitmap.Save("c:\\test.png", ImageFormat.PNG);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
if (null != g) g.Dispose();
if (null != newBitmap) newBitmap.Dispose();
}
}
}
river168 2003-08-20
  • 打赏
  • 举报
回复
随机数生成图片的ASP.net页面全部代码如下:


< %@ Page Language="C#" Debug="true" Trace="false" TraceMode="SortByCategory"% >< % @Import namespace="System.Drawing" % >< % @Import namespace="System.Drawing.Imaging" % >< % @Import namespace="System.Drawing.Text" % >< % @Import namespace="System.IO" % >< script language=C# runat=server >
public void Page_Load(object sender,EventArgs e){ string strNum=GetRandomString();
string strFontName;
int iFontSize;
int iWidth;
int iHeight;
strFontName="宋体";
iFontSize=12;
iWidth=10*strNum.Length;
iHeight=25;

Color bgColor=Color.Yellow;
Color foreColor=Color.Red;

Font foreFont=new Font(strFontName,iFontSize,FontStyle.Bold);

Bitmap Pic=new Bitmap(iWidth,iHeight,PixelFormat.Format32bppArgb);
Graphics g=Graphics.FromImage(Pic);
Rectangle r=new Rectangle(0,0,iWidth,iHeight);

g.FillRectangle(new SolidBrush(bgColor),r);

g.DrawString(strNum,foreFont,new SolidBrush(foreColor),2,2);
MemoryStream mStream=new MemoryStream();
Pic.Save(mStream,ImageFormat.Gif);
g.Dispose();
Pic.Dispose();

Response.ClearContent();
Response.ContentType="image/GIF";
Response.BinaryWrite(mStream.ToArray());
Response.End();
}
private int []GetRandomArray(int Length,int Up,int Down)
{
int iFirst=0;
int []rtArray=new Int32[Length];
Random ro=new Random(Length*unchecked((int)DateTime.Now.Ticks));
iFirst=ro.Next(Up,Down);
rtArray[0]=iFirst;
for(int i=1;i< Length;i++)
{
Random ri=new Random(i*iFirst*unchecked((int)DateTime.Now.Ticks));
rtArray[i]=ri.Next(Up,Down);
iFirst=rtArray[i];
}
return rtArray;
}
cyp503 2003-08-20
  • 打赏
  • 举报
回复
随机生成就不用说了

关于输出

你可以参考这里



http://www.aspxcn.com/dotnetarticle/html/90.html

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧