/// <summary> /// 创建地图 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CreateButton_Click(object sender, EventArgs e) { { // 记录图形选项 if (nSitesBox.Text != "") { try { numSites = int.Parse(nSitesBox.Text); } catch (Exception) { numSites = 6000; } } else { numSites = 6000; } if (nLloydBox.Text != "") { try { numLloyd = int.Parse(nLloydBox.Text); } catch (Exception) { numLloyd = 2; } } else { numLloyd = 2; } seed = seedGen.Next(); seedBox.Text = seed + ""; moreRandom = RHeightCheck.Checked; } // 生成图形对象 voronoiGraph = CreateVoronoiGraph(bounds, numSites, numLloyd, seed, moreRandom); // 渲染图形 Bitmap img = voronoiGraph.CreateMap(g, drawBiomes, drawRivers, drawSites, drawCorners, drawDelaunay, noisyEdges, smoothBlending, lighting); Bitmap noiseImg = CreateNoiseMap(); finalImg = new Bitmap(bounds, bounds); Graphics tmpGraphics = Graphics.FromImage(finalImg); tmpGraphics.DrawImage(img, new PointF(0, 0)); tmpGraphics.DrawImage(noiseImg, new PointF(0, 0)); MapImageBox.Image = finalImg; MapPanel.Refresh(); }
public static void SaveAsPng(VoronoiGraph graph, string fileName) { using (var image = graph.CreateMap()) using (var stream = new MemoryStream(image.Width * image.Height * 4)) { image.SaveAsPng(stream); stream.Seek(0, SeekOrigin.Begin); using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { stream.CopyTo(fileStream); } } }