在 ASP.NET 网页中显示动态生成的图片.doc

上传人:rrsccc 文档编号:8937073 上传时间:2021-01-26 格式:DOC 页数:9 大小:139.50KB
返回 下载 相关 举报
在 ASP.NET 网页中显示动态生成的图片.doc_第1页
第1页 / 共9页
在 ASP.NET 网页中显示动态生成的图片.doc_第2页
第2页 / 共9页
在 ASP.NET 网页中显示动态生成的图片.doc_第3页
第3页 / 共9页
在 ASP.NET 网页中显示动态生成的图片.doc_第4页
第4页 / 共9页
在 ASP.NET 网页中显示动态生成的图片.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《在 ASP.NET 网页中显示动态生成的图片.doc》由会员分享,可在线阅读,更多相关《在 ASP.NET 网页中显示动态生成的图片.doc(9页珍藏版)》请在三一文库上搜索。

1、在 ASP.NET 网页中显示动态生成的图片概述在上一篇随笔“【算法】从推箱子的解答步骤还原关卡地图”中,我给出一个控制台应用程序,将 LURD 数据转换为 XSB 数据。为了方便使用,我编写了一个 ASP.NET 网页实现从推箱子的解答步骤还原关卡地图:Sokoban: Lurd to Xsb,如下所示:=源程序代码首先是 lurd2xsb.aspx 源程序文件:01: 02: 03: 04: 05: 06: Sokoban: Lurd to Xsb07: 08: 09: 10: 11: 12: Introduction to XSB and LURD13: SokoPlayer HTML5

2、14: SokoEditor HTML515: Algorithm16: 17: 18: 19: 21: 22: 23: 24: 25: 26: 27: I would like to thank Borgar 28: orsteinsson for his kind permission using the elegant skin designed by him.29: 30: 31: 32: 注意上述源程序文件中第 26 行使用了 HTML 的 标签,而没有使用 ASP.NET 的 控件。这是因为后者只能通过其 ImageUri 属性来显示图像。而我们的图像是根据 Xsb 数据动态生成的

3、,这就不符合要求了。相应的 lurd2xsb.aspx.cs 源程序文件如下所示:01: using System;02: using System.IO;03: using System.Web.UI;04: using System.Web.UI.WebControls;05: using Skyiv.Ben.Game;06: 07: namespace Skyiv.Ben.Web08: 09: public class Lurd2xsbPage : Page10: 11: protected TextBox tbxLurd;12: protected TextBox tbxMoves;1

4、3: protected TextBox tbxXsb;14: 15: public void BtnUriHome_Click(object sender, EventArgs e)16: 17: Response.Redirect(/);18: 19: 20: public void BtnSubmit_Click(object sender, EventArgs e)21: 22: try23: 24: var runer = new Lurd2Xsb();25: var xsb = runer.GetXsbFromLurd(tbxLurd.Text);26: tbxMoves.Text

5、 = string.Format(0:N0 moves, 1:N0 pushes, runer.Moves, runer.Pushes);27: tbxXsb.Text = xsb;28: Sessionmap = Xsb2Bitmap.GetMap(xsb);29: 30: catch (Exception ex)31: 32: tbxXsb.Text = Error: + ex.Message;33: 34: 35: 36: 上述源程序代码的第 28 行将表示关卡地图的二维的字节数组存放到 Session 中,以便后面动态生成关卡地图的图像时使用。上述源程序第 24 行中使用到的 Skyi

6、v.Ben.Game.Lurd2Xsb 类位于 lurd2xsb.cs 源程序文件中:001: using System;002: using System.Text;003: using System.Drawing;004: 005: namespace Skyiv.Ben.Game006: 007: public sealed class Lurd2Xsb008: 009: public int Moves get return Steps + Pushes; 010: public int Pushes get; private set; 011: public int Steps g

7、et; private set; 012: void SetBrick(byte, map, int x, int y) mapx, y = 8; 013: void SetFloor(byte, map, Point pt) mappt.X, pt.Y |= 1; 014: void SetGoal(byte, map, Point pt) mappt.X, pt.Y |= 2; 015: void SetBox(byte, map, Point pt) mappt.X, pt.Y |= 4; 016: void UnsetBox(byte, map, Point pt) mappt.X,

8、pt.Y &= 0xFB; 017: bool HasBox(byte, map, int x, int y) return (mapx, y & 4) != 0; 018: bool IsGoal(byte, map, int x, int y) return (mapx, y & 2) != 0; 019: bool IsFloor(byte, map, int x, int y) return (mapx, y & 1) != 0; 020: bool IsBrick(byte, map, int x, int y) return mapx, y = 8; 021: bool IsWal

9、l(byte, map, int x, int y) return mapx, y = 0; 022: bool IsBrickOrWall(byte, map, int x, int y) return IsBrick(map, x, y) | IsWall(map, x, y); 023: 024: void MarkBrick(byte, map, int x, int y)025: 026: if (!IsWall(map, x, y) return;027: if (!IsBrickOrWall(map, x - 1, y - 1) return;028: if (!IsBrickO

10、rWall(map, x - 1, y + 1) return;029: if (!IsBrickOrWall(map, x + 1, y - 1) return;030: if (!IsBrickOrWall(map, x + 1, y + 1) return;031: if (!IsBrickOrWall(map, x - 1, y) return;032: if (!IsBrickOrWall(map, x + 1, y) return;033: if (!IsBrickOrWall(map, x, y - 1) return;034: if (!IsBrickOrWall(map, x

11、, y + 1) return;035: SetBrick(map, x, y);036: 037: 038: int GetX(byte, map, int y0, int y1, int x, int x2)039: 040: for (var y = y0; y y1; y+)041: if (!IsBrick(map, x, y) return x;042: return x2;043: 044: 045: int GetY(byte, map, int x0, int x1, int y, int y2)046: 047: for (var x = x0; x x1; x+)048:

12、 if (!IsBrick(map, x, y) return y;049: return y2;050: 051: 052: Rectangle GetRectangle(byte, map)053: 054: int x0 = 1, y0 = 1, x1 = map.GetLength(0) - 2, y1 = map.GetLength(1) - 2;055: x0 = GetX(map, y0, y1, x0, x0 + 1);056: x1 = GetX(map, y0, y1, x1, x1 - 1); 057: y0 = GetY(map, x0, x1, y0, y0 + 1)

13、;058: y1 = GetY(map, x0, x1, y1, y1 - 1);059: return Rectangle.FromLTRB(x0, y0, x1, y1);060: 061: 062: string GetXsb(byte, map, Point man)063: 064: var rect = GetRectangle(map);065: for (var y = rect.Top; y = rect.Bottom; y+)066: for (var x = rect.Left; x = rect.Right; x+)067: MBrick(map, x, y);068:

14、 rect = GetRectangle(map);069: var xsb = new StringBuilder();070: for (var y = rect.Top; y = rect.Bottom; y+)071: 072: for (var x = rect.Left; x = 0; i-)121: 122: switch (lurdi)123: 124: case l: man.X+; SetFloor(map, man); Steps+; break;125: case r: man.X-; SetFloor(map, man); Steps+; break;126: cas

15、e u: man.Y+; SetFloor(map, man); Steps+; break;127: case d: man.Y-; SetFloor(map, man); Steps+; break;128: case L: LeftOrRight(map, ref man, -1); Pushes+; break;129: case R: LeftOrRight(map, ref man, 1); Pushes+; break;130: case U: UpOrDown(map, ref man, -1); Pushes+; break;131: case D: UpOrDown(map

16、, ref man, 1); Pushes+; break;132: 133: 134: return map;135: 136: 137: Rectangle GetBoundary(string lurd)138: 139: var boundary = new Rectangle(0, 0, 1, 1);140: var man = new Point();141: for (var i = lurd.Length - 1; i = 0; i-)142: 143: switch (lurdi)144: 145: case l: case L: man.X+; if (boundary.R

17、ight man.X) boundary.Width+; boundary.X-; break;147: case u: case U: man.Y+; if (boundary.Bottom man.Y) boundary.Height+; boundary.Y-; break;149: 150: 151: return boundary;152: 153: 154: public string GetXsbFromLurd(string lurd)155: 156: var boundary = GetBoundary(lurd);157: var size = new Size(boun

18、dary.Width + 6, boundary.Height + 6);158: var man = new Point(3 - boundary.X, 3 - boundary.Y);159: var map = GetMap(lurd, size, ref man);160: return GetXsb(map, man);161: 162: 163: 这个源程序文件已经在上一篇随笔“【算法】从推箱子的解答步骤还原关卡地图”中介绍过了。下面是 xsb2bitmap.cs 源程序文件:01: using System;02: using System.Drawing;03: using S

19、ystem.Reflection;04: 05: namespace Skyiv.Ben.Game06: 07: public sealed class Xsb2Bitmap08: 09: static Bitmap img8;10: static Size boxSize;11: byte, map;12: 13: static Xsb2Bitmap()14: 15: img8 = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream(PushBox24.png);16: boxSize = new Size

20、(img8.Width / 8, img8.Height);17: 18: 19: public static byte, GetMap(string xsb)20: 21: var lines = xsb.Split(default(Char), StringSplitOptions.RemoveEmptyEntries);22: var map = new bytelines0.Length, lines.Length;23: for (var y = 0; y map.GetLength(1); y+)24: for (var x = 0; x map.GetLength(0); x+)

21、25: mapx, y = GetByte(linesyx);26: return map;27: 28: 29: static byte GetByte(char c)30: 31: switch (c)32: 33: case :34: case -: return 0;35: case .: return 1;36: case #: return 2;37: case _: return 3;38: case $: return 4;39: case *: return 5;40: case : return 6;41: case +: return 7;42: 43: throw ne

22、w Exception(invalid XSB char: + c + );44: 45: 46: public Xsb2Bitmap(byte, map)47: 48: this.map = map;49: 50: 51: public Bitmap GetBitmap()52: 53: if (map = null) return new Bitmap(1, 1);54: var img = new Bitmap(boxSize.Width * map.GetLength(0), boxSize.Height * map.GetLength(1);55: var graphics = Gr

23、aphics.FromImage(img);56: for (var y = 0; y map.GetLength(1); y+)57: for (var x = 0; x map.GetLength(0); x+)58: DrawBox(graphics, mapx, y, x * boxSize.Width, y * boxSize.Height);59: return img;60: 61: 62: void DrawBox(Graphics graphics, int idx, int x, int y)63: 64: graphics.DrawImage(img8, x, y, ne

24、w Rectangle(idx * boxSize.Width, 0, boxSize.Width, boxSize.Height), GraphicsUnit.Pixel);65: 66: 67: 上述源程序有两个用途:静态的 GetMap 方法将 Xsb 数据(文件格式)转换为二维字节数组GetBitmap 方法从上述二维字节数组动态生成表示关卡地图的图片下面就是用于动态生成图像的 xsbbitmap.aspx 源程序文件了:01: 02: 03: 04: 05: Xsb Bitmap06: 07: 08: 09: 10: 11: 对应的 xsbbitmap.aspx.cs 源程序文件如下

25、所示:01: using System;02: using System.IO;03: using System.Drawing.Imaging;04: using System.Web.UI;05: using System.Web.UI.WebControls;06: using Skyiv.Ben.Game;07: 08: namespace Skyiv.Ben.Web09: 10: public class XsbBitmapPage : Page11: 12: public void Page_Load(object sender, EventArgs e)13: 14: Creat

26、eImage(byte,)Sessionmap);15: 16: 17: void CreateImage(byte, map)18: 19: Sessionmap = null;20: var img = new Xsb2Bitmap(map).GetBitmap();21: try22: 23: var ms = new MemoryStream();24: img.Save(ms, ImageFormat.Png);25: Response.CContent();26: Response.ContentType = image/Png;27: Response.BinaryWrite(ms.ToArray(); 28: 29: finally30: 31: img.Dispose();32: Response.End();33: 34: 35: 36: 上述源程序第 14 行从前面赋值的 Session 取得表示关卡地图的二维字节数组,然后在第 20 行调用 Xsb2Bitmap 类的 GetBitmap 方法得到一个 Bitmap 图像,接着就是通过 Page 类的 Response 属性(类型为 HttpResponse 类)调用 BinaryWrite 方法将图像数据写入 HTTP 输出流。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1