标签:
1.向服务器请求返回图片,浏览器是不知道服务上有这个图片的存在的,只是发出请求,接收请求,显示图片
string path = context.Server.MapPath("~/1.jpg");//获取服务器上的图片并转换为本地路径 //显示到输出流中 using (Stream instream = File.OpenRead(path)) { instream.CopyTo(context.Response.OutputStream); }
2.动态的向服务器创建一张jpeg图片:
string name = context.Request["name"];//name的值可以动态的输入 using (Bitmap bmp = new Bitmap(500, 500))//创建一个尺寸为500*500的内存图片 using (Graphics g = Graphics.FromImage(bmp))//得到图片的画布 { g.DrawString(name, new Font(FontFamily.GenericSerif, 30), Brushes.Red, 0, 0);//Font应该被释放 g.DrawEllipse(Pens.Green, 100, 100, 100, 100); bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);//图片保存到输出流 }
3.获取系统信息:
context.Response.ContentType = "image/jpeg"; using (Bitmap bmp = new Bitmap(500, 200))//创建一个尺寸为500*500的内存图片 using (Graphics g = Graphics.FromImage(bmp))//得到图片的画布 using (Font font = new Font(FontFamily.GenericSerif, 30)) { HttpRequest request = context.Request; g.DrawString("IP:" + request.UserHostAddress, font, Brushes.Red, 0, 0); g.DrawString("浏览器:" + request.Browser.Browser + request.Browser.Version, font, Brushes.Red, 0, 50); g.DrawString("操作系统:" + request.Browser.Platform, font, Brushes.Red, 0, 100); bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);//图片保存到输出流 }
4.动态创建泡妞证:
context.Response.ContentType = "image/jpeg"; string name = context.Request["name"]; string imgFullPath = context.Server.MapPath("~/PaoNiuZheng.jpg"); using (Image bmp = Bitmap.FromFile(imgFullPath)) using (Graphics g = Graphics.FromImage(bmp))//得到图片的画布 using (Font font1 = new Font(FontFamily.GenericSerif, 12)) using (Font font2 = new Font(FontFamily.GenericSerif, 5)) { { g.DrawString(name, font1, Brushes.Black, 125, 220);//Font应该被释放 g.DrawString(name, font2, Brushes.Black, 309, 50);//Font应该被释放 bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);//图片保存到输出流 } }
5.动态生成四位数字的验证码
Random name = new Random(); int r = name.Next(999, 9999); using (Bitmap bmp = new Bitmap(100, 50))//创建一个尺寸为500*500的内存图片 using (Graphics g = Graphics.FromImage(bmp))//得到图片的画布 { g.DrawString(r.ToString(), new Font(FontFamily.GenericSerif, 30), Brushes.Red, 0, 0);//Font应该被释放 g.DrawEllipse(Pens.Green, 100, 100, 100, 100); bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);//图片保存到输出流 }
标签:
原文地址:http://www.cnblogs.com/genesis/p/4663838.html