diff --git a/Web/Controllers/LeafCharterController.cs b/Web/Controllers/LeafCharterController.cs index b61a98d..c3f9fac 100644 --- a/Web/Controllers/LeafCharterController.cs +++ b/Web/Controllers/LeafCharterController.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Drawing; +using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Web.Mvc; @@ -25,7 +27,7 @@ namespace LeafWeb.Web.Controllers return View(leafInputId); } - public ActionResult LeafCharts(int leafInputId, int number) + public ActionResult LeafCharts(int leafInputId) { var leafOutputFile = DataService @@ -36,30 +38,68 @@ namespace LeafWeb.Web.Controllers if (leafOutputFile == null) throw new ArgumentOutOfRangeException(); // TODO: break - return File(GetChartImage(leafOutputFile.Contents, number), "image/png", "mychart.png"); + var curveData = GetCurveData(leafOutputFile.Contents); + + var charts = GetChartBitmaps(curveData).ToList(); + + var combinedChart = CombineBitmaps(charts); + + foreach (var chart in charts) chart.Dispose(); // cleanup + + using (var ms = new MemoryStream()) + { + combinedChart.Save(ms, ImageFormat.Png); + ms.Seek(0, SeekOrigin.Begin); + return File(ms.ToArray(), "image/png", "mychart.png"); + } } - private byte[] GetChartImage(byte[] fileContents, int number, ChartImageFormat format = ChartImageFormat.Png) + private CurveData[] GetCurveData(byte[] fileContents) { LeafGasComparison[] leafGasComparisons; using (var parser = new LeafGasComparisonParser(fileContents)) leafGasComparisons = parser.Parse(); - var curveData = CurveDataConverter.Convert(leafGasComparisons).ToArray(); + return CurveDataConverter.Convert(leafGasComparisons).ToArray(); + } + private IEnumerable GetChartBitmaps(CurveData[] curveData) + { var charts = LeafWebCharter.ProduceCharts(curveData[0]); - - //Image.FromFile() - - using (var ms = new MemoryStream()) + + foreach (var chart in charts) { - charts.Skip(number).First().SaveImage(ms, format); + // http://stackoverflow.com/a/336396/99492 + var ms = new MemoryStream(); // this gets attached to the bmp, dispose of it later + chart.SaveImage(ms, ChartImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); - - return ms.ToArray(); + yield return new Bitmap(ms); } } + private Bitmap CombineBitmaps(IList bitmaps, int columnCount = 2) + { + var cellWidth = bitmaps[0].Width; + var cellHeight = bitmaps[0].Height; + var width = cellWidth * 2; + var height = cellHeight * bitmaps.Count / 2; + + var combinedBitmap = new Bitmap(width, height); + using (var g = Graphics.FromImage(combinedBitmap)) + { + var currentCol = 0; + var currentRow = 0; + foreach (var image in bitmaps) + { + g.DrawImage(image, currentCol * cellWidth, currentRow * cellHeight); + + currentCol = (currentCol + 1)%columnCount; + currentRow += currentCol == 0 ? 1 : 0; + } + } + return combinedBitmap; + } + public ActionResult ChartSample() { var chart = new Chart diff --git a/Web/Views/LeafCharter/Index.cshtml b/Web/Views/LeafCharter/Index.cshtml index 60fd503..980a233 100644 --- a/Web/Views/LeafCharter/Index.cshtml +++ b/Web/Views/LeafCharter/Index.cshtml @@ -1,7 +1,3 @@ -@using LeafWeb.Web.Charter -@model int +@model int -@for (int i = 0; i < 8; i++) -{ - image -} \ No newline at end of file +image