public MazeGraphic Paint(Maze maze, MazeGenerationOptions options) { ImageSizesHelper helper = new ImageSizesHelper(); ImageSizes sizes = helper.CalculateSizes(maze, options); using (Bitmap bitmap = new Bitmap(sizes.Width, sizes.Height)) { using (Graphics graphics = Graphics.FromImage(bitmap)) { this.DrawMaze(maze, graphics, sizes); using (MemoryStream memoryStream = new MemoryStream()) { bitmap.Save(memoryStream, ImageFormat.Jpeg); return(new MazeGraphic { Name = Guid.NewGuid().ToString(GuidFormats.Digits), GraphicType = "image", Content = memoryStream.ToArray() }); } } } }
private void DrawMaze(Maze maze, Graphics graphics, ImageSizes sizes) { Pen pen = new Pen(Color.White, 2); float currentPositionX = sizes.MarginX; float currentPositionY = sizes.MarginY; float width = sizes.CellWidth; float height = sizes.CellHeight; for (int i = 0; i < maze.Length; i++) { currentPositionY = sizes.MarginY; for (int j = 0; j < maze[i].Length; j++) { MazeCell currentCell = maze[i][j]; if (currentCell.HasWall(MazeWall.Bottom)) { graphics.DrawLine(pen, currentPositionX, currentPositionY, currentPositionX + width, currentPositionY); } if (currentCell.HasWall(MazeWall.Right)) { graphics.DrawLine(pen, currentPositionX + width, currentPositionY, currentPositionX + width, currentPositionY + height); } if (currentCell.HasWall(MazeWall.Top)) { graphics.DrawLine(pen, currentPositionX + width, currentPositionY + height, currentPositionX, currentPositionY + height); } if (currentCell.HasWall(MazeWall.Left)) { graphics.DrawLine(pen, currentPositionX, currentPositionY + height, currentPositionX, currentPositionY); } currentPositionY += sizes.CellHeight; } currentPositionX += sizes.CellWidth; } }
public ImageSizes CalculateSizes(Maze maze, MazeGenerationOptions options) { ImageSizes result = new ImageSizes(); if (options.FixedSize) { result.Width = (int)(sizeX + 2 * marginX); result.Height = (int)(sizeY + 2 * marginY); result.CellWidth = (int)(sizeX / maze.Length); result.CellHeight = (int)(sizeY / maze[0].Length); } else { result.CellWidth = 25; result.CellHeight = 25; result.Width = (int)(marginX * 2 + result.CellWidth * maze.Length); result.Height = (int)(marginY * 2 + result.CellHeight * maze[0].Length); } result.MarginX = marginX; result.MarginY = marginY; return(result); }