33
44using System ;
55using System . Collections . Generic ;
6+ using System . Drawing ;
7+ using System . Drawing . Imaging ;
68using System . Globalization ;
9+ using System . IO ;
710using System . Linq ;
811using System . Net . Http ;
912using System . Text ;
@@ -35,6 +38,10 @@ public sealed class AzureMapsGeoSpatialService : IGeoSpatialService
3538 private static readonly string GetRouteDirections = $ "https://atlas.microsoft.com/route/directions/json?&api-version=1.0&instructionsType=text&query={{0}}&maxAlternatives={{1}}";
3639 private static readonly string GetRouteDirectionsWithRouteType = $ "https://atlas.microsoft.com/route/directions/json?&api-version=1.0&instructionsType=text&query={{0}}&&routeType={{1}}&maxAlternatives={{2}}";
3740 private static string apiKey ;
41+
42+ // TODO if set to 0, url will be used to generate image directly. However the url contains subscription key.
43+ // If set > 0, url will be converted to jpg as data uri, so subscription key is hidden.
44+ private static long useDataUriQuality = 75 ;
3845 private static string userLocale ;
3946 private static HttpClient httpClient ;
4047
@@ -178,7 +185,7 @@ public async Task<List<PointOfInterestModel>> GetPointOfInterestListByParkingCat
178185 /// </summary>
179186 /// <param name="pointOfInterest">The point of interest model.</param>
180187 /// <returns>PointOfInterestModel.</returns>
181- public Task < PointOfInterestModel > GetPointOfInterestDetailsAsync ( PointOfInterestModel pointOfInterest , int width = 0 , int height = 0 )
188+ public async Task < PointOfInterestModel > GetPointOfInterestDetailsAsync ( PointOfInterestModel pointOfInterest , int width = 0 , int height = 0 )
182189 {
183190 string imageUrl = string . Format (
184191 CultureInfo . InvariantCulture ,
@@ -189,9 +196,14 @@ public Task<PointOfInterestModel> GetPointOfInterestDetailsAsync(PointOfInterest
189196 width <= 0 ? ImageWidth : width ,
190197 height <= 0 ? ImageHeight : height ) + "&subscription-key=" + apiKey ;
191198
199+ if ( useDataUriQuality > 0 )
200+ {
201+ imageUrl = await ConvertToDataUri ( imageUrl ) ;
202+ }
203+
192204 pointOfInterest . PointOfInterestImageUrl = imageUrl ;
193205
194- return Task . FromResult ( pointOfInterest ) ;
206+ return pointOfInterest ;
195207 }
196208
197209 /// <summary>
@@ -249,7 +261,14 @@ public async Task<string> GetRouteImageAsync(PointOfInterestModel destination, R
249261
250262 string pins = string . Format ( CultureInfo . InvariantCulture , RoutePins , PointOfInterestSharedStrings . START , route . Legs [ 0 ] . Points [ 0 ] . Longitude , route . Legs [ 0 ] . Points [ 0 ] . Latitude , PointOfInterestSharedStrings . END , destination . Geolocation . Longitude , destination . Geolocation . Latitude ) ;
251263
252- return string . Format ( CultureInfo . InvariantCulture , ImageUrlForRoute , zoom , centerLongitude , centerLatitude , pins , sb . ToString ( ) , width , height ) + "&subscription-key=" + apiKey ;
264+ var imageUrl = string . Format ( CultureInfo . InvariantCulture , ImageUrlForRoute , zoom , centerLongitude , centerLatitude , pins , sb . ToString ( ) , width , height ) + "&subscription-key=" + apiKey ;
265+
266+ if ( useDataUriQuality > 0 )
267+ {
268+ imageUrl = await ConvertToDataUri ( imageUrl ) ;
269+ }
270+
271+ return imageUrl ;
253272
254273 void AddPoint ( double longitude , double latitude )
255274 {
@@ -407,5 +426,19 @@ private async Task<List<PointOfInterestModel>> GetPointsOfInterestAsync(string u
407426
408427 return pointOfInterestList ;
409428 }
429+
430+ private async Task < string > ConvertToDataUri ( string url )
431+ {
432+ MemoryStream ms = new MemoryStream ( ) ;
433+ using ( var image = Image . FromStream ( await httpClient . GetStreamAsync ( url ) ) )
434+ {
435+ var encoder = ImageCodecInfo . GetImageDecoders ( ) . Where ( x => x . FormatID == ImageFormat . Jpeg . Guid ) . FirstOrDefault ( ) ;
436+ var encoderParameters = new EncoderParameters ( 1 ) ;
437+ encoderParameters . Param [ 0 ] = new EncoderParameter ( System . Drawing . Imaging . Encoder . Quality , useDataUriQuality ) ;
438+ image . Save ( ms , encoder , encoderParameters ) ;
439+ }
440+
441+ return $ "data:image/jpeg;base64,{ Convert . ToBase64String ( ms . ToArray ( ) ) } ";
442+ }
410443 }
411444}
0 commit comments