private void SaveRasterToFile(RtRaster <double> raster, RtColorPalette palette, string name)
        {
            //raster.CellSize = 2;
            var    bmpAndPalette = GetRasterBitmap(raster, palette);
            Bitmap bmp           = bmpAndPalette.Item1;

            bmp.Save(name, ImageFormat.Png);
        }
        public static Tuple <RtColorPalette, Quantile[], string> GeneratePaletteAndHistogramForData(IDataService _dataService, RtRaster <double> raster, bool ignoreZero = true, Action <int, string> trace = null)
        {
            var rasterBand = raster.Bands[0] as IRasterBand <double>;
            var distinct   = rasterBand.RasterData.Where(v => v != rasterBand.NoDataValue && v != double.MinValue)
                             .Distinct().OrderBy(x => x)
                             .ToArray();

            var    cp    = new RtColorPalette();
            int    index = 0;
            double hMin  = double.MinValue;

            if (distinct.Length > 0)
            {
                hMin = distinct[index];
                while (hMin == double.MinValue)
                {
                    index++;
                    if (index < distinct.Length)
                    {
                        hMin = distinct[index++];
                    }
                    else
                    {
                        break;
                    }
                }
                if (hMin == double.MinValue)
                {
                    //Defer exception creation until later
                    return(Tuple.Create(cp, new Quantile[] { }, "No values in Heatmap from which to generate a histogram"));
                }
            }
            else
            {
                //Defer exception creation until later
                return(Tuple.Create(cp, new Quantile[] { }, "No values in Heatmap from which to generate a histogram"));
            }

            float hMax = (float)distinct[distinct.Length - 1];

            if (trace != null)
            {
                trace(5, String.Format("Requesting Histogram generation with Min = {0} and Max = {1} and number of distinct values = {2}", hMin, hMax, distinct.Count()));
            }

            var allColors = _dataService.GetColors(int.MaxValue);

            float[] valuesToIgnore = ignoreZero ? new float[] { 0, (float)rasterBand.NoDataValue } : new float[] { (float)rasterBand.NoDataValue };
            var     quantiles      = Histogram.GetHistogram((float)hMin, (float)hMax, rasterBand.RasterData.Select(v => (float)v).ToArray(), allColors.Length, valuesToIgnore, true, trace);

            //var colors = new[] { 0xF92525, 0xF96C25, 0xF98F25, 0xF99625, 0xF9A425, 0xF9C825, 0xF9D625, 0xF9F225, 0xEBF925, 0xE4F925, 0xCFF925, 0xB2F925, 0x88F925, 0x48FD1B, 0x1BFD22, 0x04FF58 };
            if (quantiles.Length > 0)
            {
                var colors = allColors.SelectValues(quantiles.Length).ToArray();
                var j      = 0;
                for (int i = 0; i < quantiles.Length; i++)
                {
                    var   q             = quantiles[i];
                    Color colorToAssign = Color.FromArgb(230, Color.FromArgb(colors[j]));
                    quantiles[i].Color = Color.FromArgb(230, Color.FromArgb(colors[j]));
                    cp.AddColorBand(q.Min, q.Max, j, colorToAssign, q);
                    j++;
                }
            }
            cp.PaletteType      = RtColorPalette.ColorPaletteType.Range;
            cp.MissingBandColor = Color.Transparent;

            return(Tuple.Create(cp, quantiles, "Success"));
        }
        private Tuple <Bitmap, RtColorPalette> GetRasterBitmap(RtRaster <double> raster, RtColorPalette palette)
        {
            if (palette != null)
            {
                var image = raster.ToImage(palette, Color.AntiqueWhite);
                return(Tuple.Create(image, palette));
            }

            return(Tuple.Create(null as Bitmap, null as RtColorPalette));
        }