Exemplo n.º 1
0
 public static byte[] WebpToPng(byte[] webpData)
 {
     using (var origBmp = SKBitmap.Decode(webpData))
         using (var stream = new SKDynamicMemoryWStream())
         {
             origBmp.Encode(stream, SKEncodedImageFormat.Png, 100);
             return(stream.DetachAsData().ToArray());
         }
 }
Exemplo n.º 2
0
 private byte[] Convert(byte[] orig, SKEncodedImageFormat format)
 {
     using (var origBmp = SKBitmap.Decode(orig))
         using (var stream = new SKDynamicMemoryWStream())
         {
             origBmp.Encode(stream, format, 100);
             return(stream.DetachAsData().ToArray());
         }
 }
Exemplo n.º 3
0
        public string GetThumbnail(SKBitmap image, int width, int height)
        {
            using (var scaledImage = ResizeImage(image, width, height))
            {
                SKBitmap.Resize(scaledImage, image, resizeMethod);

                using (var memoryStream = new SKDynamicMemoryWStream())
                {
                    scaledImage.Encode(memoryStream, SKEncodedImageFormat.Jpeg, quality);
                    return(Convert.ToBase64String(memoryStream.DetachAsData().ToArray()));
                }
            }
        }
Exemplo n.º 4
0
        private Response RenderMap(dynamic parameters)
        {
            var gameServer = this.servers.OfType <IGameServer>().FirstOrDefault(s => s.Id == (byte)parameters.serverId);

            var map = gameServer?.ServerInfo.Maps.FirstOrDefault(m => m.MapNumber == (short)parameters.mapId);

            if (map == null)
            {
                Log.Warn($"requested map not available. map number: {parameters.mapId}; server id: {parameters.serverId}");
                return(null);
            }

            var terrain = new GameMapTerrain(map.MapName, map.TerrainData);

            using (var bitmap = new SkiaSharp.SKBitmap(0x100, 0x100))
            {
                for (int y = 0; y < 0x100; y++)
                {
                    for (int x = 0; x < 0x100; x++)
                    {
                        var color = SKColors.Black;
                        if (terrain.SafezoneMap[y, x])
                        {
                            color = SKColors.Gray;
                        }
                        else if (terrain.WalkMap[y, x])
                        {
                            color = SKColors.SpringGreen;
                        }

                        bitmap.SetPixel(x, y, color);
                    }
                }

                using (var memoryStream = new SKDynamicMemoryWStream())
                {
                    if (SKPixmap.Encode(memoryStream, bitmap, SKEncodedImageFormat.Png, 100))
                    {
                        return(this.Response.FromStream(memoryStream.DetachAsData().AsStream, "image/png"));
                    }
                }

                return(null);
            }
        }
Exemplo n.º 5
0
        private Stream RenderMap(IGameMapInfo map)
        {
            var terrain = new GameMapTerrain(map.MapName, map.TerrainData);

            using (var bitmap = new SkiaSharp.SKBitmap(0x100, 0x100))
            {
                for (int y = 0; y < 0x100; y++)
                {
                    for (int x = 0; x < 0x100; x++)
                    {
                        var color = SKColors.Black;
                        if (terrain.SafezoneMap[y, x])
                        {
                            color = SKColors.Gray;
                        }
                        else if (terrain.WalkMap[y, x])
                        {
                            color = SKColors.SpringGreen;
                        }
                        else
                        {
                            // we use the default color.
                        }

                        bitmap.SetPixel(x, y, color);
                    }
                }

                using (var memoryStream = new SKDynamicMemoryWStream())
                {
                    if (SKPixmap.Encode(memoryStream, bitmap, SKEncodedImageFormat.Png, 100))
                    {
                        return(memoryStream.DetachAsData().AsStream());
                    }
                }

                return(null);
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            // load some routing data and build a routing network.
            var routerDb = new RouterDb();

            using var stream = File.Open(@"C:\Users\yukik\Downloads\zuid-holland-latest.osm.pbf", FileMode.Open, FileAccess.Read);
            var src = new PBFOsmStreamSource(stream);

            routerDb.LoadOsmData(src, Vehicle.Bicycle); // create the network for Bicycle only.

            // create a router.
            var router = new Router(routerDb);

            // get a profile.
            var profile = Vehicle.Bicycle.Fastest(); // the default OSM Bicycle profile.

            // create a routerpoint from a location.
            // snaps the given location to the nearest routable edge.
            var start = router.Resolve(profile, 52.154820f, 4.479307f);
            var end   = router.Resolve(profile, 52.160942f, 4.495821f);

            // calculate a route.
            var route = router.Calculate(profile, start, end);

            var retList = new List <(float lat, float lon, float time, float distance, string name)>();


            // Create the path
            var path = new SKPath();

            var firstShape = route.Shape[0];

            path.MoveTo(5, 5);

            var strokePaint = new SKPaint
            {
                Style       = SKPaintStyle.Stroke,
                Color       = SKColors.Black,
                StrokeWidth = 1
            };

            int scale = 50000;

            for (int i = 1; i < route.ShapeMeta.Length - 2; i++)
            {
                var meta = route.ShapeMeta[i];

                var strn    = meta.Attributes.TryGetValue("name", out var desc);
                var current = route.Shape[meta.Shape];
                if (meta.Shape > 0 && route.ShapeMeta[i - 1].Attributes.TryGetValue("name", out var v) && v == desc)
                {
                    continue;
                }

                if (route.Shape.Length <= meta.Shape + 1)
                {
                    continue;
                }
                var lat = (current.Latitude - firstShape.Latitude) * scale;
                var lon = (current.Longitude - firstShape.Longitude) * scale;
                retList.Add((lat, lon, meta.Time - retList.LastOrDefault().time, meta.Distance - retList.LastOrDefault().distance, desc));

                path.LineTo(lon, -lat);
            }

            var rect = path.Bounds;
            var translationMatrix = SKMatrix.CreateTranslation(Math.Abs(path.Bounds.Left) + 20, Math.Abs(path.Bounds.Top) + 20);

            path.Transform(translationMatrix);
            rect.Inflate(100, 10);
            using var outStream = new SKDynamicMemoryWStream();
            var svg = SKSvgCanvas.Create(rect, outStream);


            svg.DrawPath(path, strokePaint);

            foreach (var(lat, lon, time, distance, name) in retList)
            {
                var point = new SKPoint(lon, -lat);

                svg.DrawText($"{name} ({TimeSpan.FromSeconds(time).Humanize()} - {Math.Round(distance)} meter) ", translationMatrix.MapPoint(point), strokePaint);
            }


            var s = outStream.DetachAsData();

            File.WriteAllBytes("test.svg", s.ToArray());

            Console.WriteLine("Hello World!");
        }