/// <summary> /// Function to get BitmapImage from RuntimeImage /// </summary> /// <param name="rtImage">Runtime Image</param> /// <returns>Bitmap Image</returns> private async Task <BitmapImage> GetImageAsync(RuntimeImage rtImage) { if (rtImage != null) { try { if (rtImage.LoadStatus != Esri.ArcGISRuntime.LoadStatus.Loaded) { await rtImage.LoadAsync(); } var stream = await rtImage.GetEncodedBufferAsync(); var image = new BitmapImage(); image.BeginInit(); stream.Seek(0, SeekOrigin.Begin); image.StreamSource = stream; image.CacheOption = BitmapCacheOption.OnLoad; image.EndInit(); return(image); } catch { return(null); } } return(null); }
private async void OnTakeScreenshotClicked(object sender, EventArgs e) { // Make sure an image is not in progress (check the activity indicator) if (CreatingImageIndicator.IsVisible) { return; } // Show the activity indicator while the image is being created CreatingImageIndicator.IsVisible = true; try { // Export the image from mapview and assign it to the imageview RuntimeImage exportedImage = await MyMapView.ExportImageAsync(); // Create layout for sublayers page // Create root layout StackLayout layout = new StackLayout(); Button closeButton = new Button { Text = "Close" }; closeButton.Clicked += CloseButton_Clicked; // Create image bitmap by getting stream from the exported image var buffer = await exportedImage.GetEncodedBufferAsync(); byte[] data = new byte[buffer.Length]; buffer.Read(data, 0, data.Length); var bitmap = ImageSource.FromStream(() => new MemoryStream(data)); Image image = new Image() { Source = bitmap, Margin = new Thickness(10) }; // Add elements into the layout layout.Children.Add(closeButton); layout.Children.Add(image); // Create internal page for the navigation page ContentPage screenshotPage = new ContentPage() { Content = layout, Title = "Screenshot" }; // Hide the activity indicator CreatingImageIndicator.IsVisible = false; // Navigate to the sublayers page await Navigation.PushAsync(screenshotPage); } catch (Exception ex) { await((Page)Parent).DisplayAlert("Error", ex.ToString(), "OK"); } }
private async void OnTakeScreenshotClicked(object sender, EventArgs e) { try { ScreenshotButton.IsEnabled = false; // Wait for rendering to finish before taking the screenshot. await WaitForRenderCompleteAsync(MyMapView); // Export the image from the map view. RuntimeImage exportedImage = await MyMapView.ExportImageAsync(); // Create layout for sublayers page // Create root layout StackLayout layout = new StackLayout(); Button closeButton = new Button { Text = "Close" }; closeButton.Clicked += CloseButton_Clicked; // Create image bitmap by getting stream from the exported image. // NOTE: currently broken on UWP due to Xamarin.Forms bug https://github.com/xamarin/Xamarin.Forms/issues/5188. var buffer = await exportedImage.GetEncodedBufferAsync(); byte[] data = new byte[buffer.Length]; buffer.Read(data, 0, data.Length); var bitmap = ImageSource.FromStream(() => new MemoryStream(data)); Image image = new Image() { Source = bitmap, Margin = new Thickness(10) }; // Add elements into the layout. layout.Children.Add(closeButton); layout.Children.Add(image); // Create internal page for the navigation page. ContentPage screenshotPage = new ContentPage() { Content = layout, Title = "Screenshot" }; // Navigate to the sublayers page. await Navigation.PushAsync(screenshotPage); } catch (Exception ex) { await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK"); } finally { ScreenshotButton.IsEnabled = true; } }
public async void UpdateMapItem() { // Save the map await _map.SaveAsync(); // Export the current map view for the item's thumbnail RuntimeImage thumbnailImg = await _mapView.ExportImageAsync(); // Get the file stream from the new thumbnail image Stream imageStream = await thumbnailImg.GetEncodedBufferAsync(); // Update the item thumbnail (_map.Item as PortalItem).SetThumbnailWithImage(imageStream); await _map.SaveAsync(); }
// Handle the OnMapInfoEntered event from the item input UI. // MapSavedEventArgs contains the title, description, and tags that were entered. private async void MapItemInfoEntered() { // Get the current map. Map myMap = _myMapView.Map; try { // Show the activity indicator so the user knows work is happening. _activityIndicator.StartAnimating(); // Get information entered by the user for the new portal item properties. string[] tags = _tags.Split(','); // Apply the current extent as the map's initial extent. myMap.InitialViewpoint = _myMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry); // Export the current map view for the item's thumbnail. RuntimeImage thumbnailImg = await _myMapView.ExportImageAsync(); // See if the map has already been saved (has an associated portal item). if (myMap.Item == null) { // Call a function to save the map as a new portal item. await SaveNewMapAsync(myMap, _title, _description, tags, thumbnailImg); // Report a successful save. UIAlertController alert = UIAlertController.Create("Saved map", "Saved " + _title + " to ArcGIS Online", UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); PresentViewController(alert, true, null); } else { // This is not the initial save, call SaveAsync to save changes to the existing portal item. await myMap.SaveAsync(); // Get the file stream from the new thumbnail image. Stream imageStream = await thumbnailImg.GetEncodedBufferAsync(); // Update the item thumbnail. ((PortalItem)myMap.Item).SetThumbnail(imageStream); await myMap.SaveAsync(); // Report update was successful. UIAlertController alert = UIAlertController.Create("Updated map", "Saved changes to " + _title, UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); PresentViewController(alert, true, null); } } catch (Exception) { // Report save error. UIAlertController alert = UIAlertController.Create("Error", "Unable to save " + _title, UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); PresentViewController(alert, true, null); } finally { // Hide the progress bar. _activityIndicator.StopAnimating(); } }
// Event handler to get information entered by the user and save the map private async void SaveMapAsync(object sender, SaveMapEventArgs e) { // Get the current map Map myMap = MyMapView.Map; try { // Show the progress bar so the user knows work is happening SaveMapProgressBar.IsVisible = true; // Make sure the user is logged in to ArcGIS Online Credential cred = await EnsureLoggedInAsync(); AuthenticationManager.Current.AddCredential(cred); // Get information entered by the user for the new portal item properties string title = e.MapTitle; string description = e.MapDescription; string[] tags = e.Tags; // Apply the current extent as the map's initial extent myMap.InitialViewpoint = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry); // Export the current map view for the item's thumbnail RuntimeImage thumbnailImage = await MyMapView.ExportImageAsync(); // See if the map has already been saved (has an associated portal item) if (myMap.Item == null) { // Get the ArcGIS Online portal (will use credential from login above) ArcGISPortal agsOnline = await ArcGISPortal.CreateAsync(new Uri(ArcGISOnlineUrl)); // Save the current state of the map as a portal item in the user's default folder await myMap.SaveAsAsync(agsOnline, null, title, description, tags, thumbnailImage); // Report a successful save await((Page)Parent).DisplayAlert("Map Saved", "Saved '" + title + "' to ArcGIS Online!", "OK"); } else { // This is not the initial save, call SaveAsync to save changes to the existing portal item await myMap.SaveAsync(); // Get the file stream from the new thumbnail image Stream imageStream = await thumbnailImage.GetEncodedBufferAsync(); // Update the item thumbnail ((PortalItem)myMap.Item).SetThumbnailWithImage(imageStream); await myMap.SaveAsync(); // Report update was successful await((Page)Parent).DisplayAlert("Updates Saved", "Saved changes to '" + myMap.Item.Title + "'", "OK"); } } catch (Exception ex) { // Show the exception message await((Page)Parent).DisplayAlert("Unable to save map", ex.Message, "OK"); } finally { // Hide the progress bar SaveMapProgressBar.IsVisible = false; } }
private async void SaveMapAsync(object sender, OnSaveMapEventArgs e) { AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this); // Get the current map Map myMap = _myMapView.Map; try { // Show the progress bar so the user knows work is happening _progressBar.Visibility = ViewStates.Visible; // Get information entered by the user for the new portal item properties string title = e.MapTitle; string description = e.MapDescription; string[] tags = e.Tags; // Apply the current extent as the map's initial extent myMap.InitialViewpoint = _myMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry); // Export the current map view for the item's thumbnail RuntimeImage thumbnailImg = await _myMapView.ExportImageAsync(); // See if the map has already been saved (has an associated portal item) if (myMap.Item == null) { // Call a function to save the map as a new portal item await SaveNewMapAsync(myMap, title, description, tags, thumbnailImg); // Report a successful save alertBuilder.SetTitle("Map saved"); alertBuilder.SetMessage("Saved '" + title + "' to ArcGIS Online!"); alertBuilder.Show(); } else { // This is not the initial save, call SaveAsync to save changes to the existing portal item await myMap.SaveAsync(); // Get the file stream from the new thumbnail image Stream imageStream = await thumbnailImg.GetEncodedBufferAsync(); // Update the item thumbnail ((PortalItem)myMap.Item).SetThumbnail(imageStream); await myMap.SaveAsync(); // Report update was successful alertBuilder.SetTitle("Updates saved"); alertBuilder.SetMessage("Saved changes to '" + myMap.Item.Title + "'"); alertBuilder.Show(); } } catch (Exception ex) { // Show the exception message alertBuilder.SetTitle("Unable to save map"); alertBuilder.SetMessage(ex.Message); alertBuilder.Show(); } finally { // Hide the progress bar _progressBar.Visibility = ViewStates.Invisible; } }
private async void SaveMapClicked(object sender, RoutedEventArgs e) { try { // Show the progress bar so the user knows work is happening SaveProgressBar.Visibility = Visibility.Visible; // Get the current map Map myMap = MyMapView.Map; // Apply the current extent as the map's initial extent myMap.InitialViewpoint = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry); // Get the current map view for the item thumbnail RuntimeImage thumbnailImg = await MyMapView.ExportImageAsync(); // See if the map has already been saved (has an associated portal item) if (myMap.Item == null) { // Get information for the new portal item string title = TitleTextBox.Text; string description = DescriptionTextBox.Text; string[] tags = TagsTextBox.Text.Split(','); // Make sure all required info was entered if (String.IsNullOrEmpty(title) || String.IsNullOrEmpty(description) || tags.Length == 0) { throw new Exception("Please enter a title, description, and some tags to describe the map."); } // Call a function to save the map as a new portal item await SaveNewMapAsync(MyMapView.Map, title, description, tags, thumbnailImg); // Report a successful save MessageBox.Show("Saved '" + title + "' to ArcGIS Online!", "Map Saved"); } else { // This is not the initial save, call SaveAsync to save changes to the existing portal item await myMap.SaveAsync(); // Get the file stream from the new thumbnail image Stream imageStream = await thumbnailImg.GetEncodedBufferAsync(); // Update the item thumbnail ((PortalItem)myMap.Item).SetThumbnail(imageStream); await myMap.SaveAsync(); // Report update was successful MessageBox.Show("Saved changes to '" + myMap.Item.Title + "'", "Updates Saved"); } } catch (Exception ex) { // Report error message MessageBox.Show("Error saving map to ArcGIS Online: " + ex.Message); } finally { // Hide the progress bar SaveProgressBar.Visibility = Visibility.Hidden; } }
private async Task ReadMobileStyle(string stylePath) { try { // Open the mobile style file at the provided path. _emojiStyle = await SymbolStyle.OpenAsync(stylePath); // Get the default style search parameters. SymbolStyleSearchParameters searchParams = await _emojiStyle.GetDefaultSearchParametersAsync(); // Search the style with the default parameters to return all symbol results. IList <SymbolStyleSearchResult> styleResults = await _emojiStyle.SearchSymbolsAsync(searchParams); // Create lists to contain the available symbol layers for each category of symbol and add an empty entry as default. List <SymbolLayerInfo> eyeSymbolInfos = new List <SymbolLayerInfo> { new SymbolLayerInfo("", null, "") }; List <SymbolLayerInfo> mouthSymbolInfos = new List <SymbolLayerInfo> { new SymbolLayerInfo("", null, "") }; List <SymbolLayerInfo> hatSymbolInfos = new List <SymbolLayerInfo>() { new SymbolLayerInfo("", null, "") }; // Loop through the results and put symbols into the appropriate list according to category. foreach (SymbolStyleSearchResult result in styleResults) { // Get the symbol for this result. MultilayerPointSymbol multiLayerSym = result.Symbol as MultilayerPointSymbol; // Create a swatch image from the symbol. RuntimeImage swatch = await multiLayerSym.CreateSwatchAsync(30, 30, 96, Color.White); // Create an image source from the swatch. Stream imageBuffer = await swatch.GetEncodedBufferAsync(); byte[] imageData = new byte[imageBuffer.Length]; imageBuffer.Read(imageData, 0, imageData.Length); ImageSource symbolImage = ImageSource.FromStream(() => new MemoryStream(imageData)); // Create a symbol layer info object to represent the symbol in the list. // The symbol key will be used to retrieve the symbol from the style. SymbolLayerInfo symbolInfo = new SymbolLayerInfo(result.Name, symbolImage, result.Key); // Add the symbol layer info to the correct list for its category. switch (result.Category) { case "Eyes": { eyeSymbolInfos.Add(symbolInfo); break; } case "Mouth": { mouthSymbolInfos.Add(symbolInfo); break; } case "Hat": { hatSymbolInfos.Add(symbolInfo); break; } } } // Show the symbols in the category list boxes. EyesListView.ItemsSource = eyeSymbolInfos; MouthListView.ItemsSource = mouthSymbolInfos; HatListView.ItemsSource = hatSymbolInfos; // Call a function to construct the current symbol (default yellow circle). Symbol faceSymbol = await GetCurrentSymbol(); // Call a function to show a preview image of the symbol. await UpdateSymbolPreview(faceSymbol); } catch (Exception ex) { // Report the exception. await DisplayAlert("Error reading style", ex.Message, "OK"); } }
// Handle the OnMapInfoEntered event from the item input UI // MapSavedEventArgs contains the title, description, and tags that were entered private async void MapItemInfoEntered(object sender, MapSavedEventArgs e) { // Get the current map var myMap = _myMapView.Map; try { // Show the activity indicator so the user knows work is happening _activityIndicator.StartAnimating(); // Get information entered by the user for the new portal item properties var title = e.Title; var description = e.Description; var tags = e.Tags; // Apply the current extent as the map's initial extent myMap.InitialViewpoint = _myMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry); // Export the current map view for the item's thumbnail RuntimeImage thumbnailImg = await _myMapView.ExportImageAsync(); // See if the map has already been saved (has an associated portal item) if (myMap.Item == null) { // Call a function to save the map as a new portal item await SaveNewMapAsync(myMap, title, description, tags, thumbnailImg); // Report a successful save UIAlertController alert = UIAlertController.Create("Saved map", "Saved " + title + " to ArcGIS Online", UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); PresentViewController(alert, true, null); } else { // This is not the initial save, call SaveAsync to save changes to the existing portal item await myMap.SaveAsync(); // Get the file stream from the new thumbnail image Stream imageStream = await thumbnailImg.GetEncodedBufferAsync(); // Update the item thumbnail (myMap.Item as PortalItem).SetThumbnailWithImage(imageStream); await myMap.SaveAsync(); // Report update was successful UIAlertController alert = UIAlertController.Create("Updated map", "Saved changes to " + title, UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); PresentViewController(alert, true, null); } } catch (Exception ex) { // Report save error UIAlertController alert = UIAlertController.Create("Error", "Unable to save " + e.Title, UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); PresentViewController(alert, true, null); } finally { // Get rid of the item input controls _mapInfoUI.Hide(); _mapInfoUI = null; // Hide the progress bar _activityIndicator.StopAnimating(); } }
private async void SaveMapClicked(object sender, RoutedEventArgs e) { try { // Don't attempt to save if the OAuth settings weren't provided if (string.IsNullOrEmpty(AppClientId) || string.IsNullOrEmpty(OAuthRedirectUrl)) { var dialog = new MessageDialog("OAuth settings were not provided.", "Cannot Save"); await dialog.ShowAsync(); SaveMapFlyout.Hide(); return; } // Show the progress bar so the user knows work is happening SaveProgressBar.Visibility = Visibility.Visible; // Get the current map var myMap = MyMapView.Map; // Apply the current extent as the map's initial extent myMap.InitialViewpoint = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry); // Export the current map view to use as the item's thumbnail RuntimeImage thumbnailImg = await MyMapView.ExportImageAsync(); // See if the map has already been saved (has an associated portal item) if (myMap.Item == null) { // Get information for the new portal item var title = TitleTextBox.Text; var description = DescriptionTextBox.Text; var tagText = TagsTextBox.Text; // Make sure all required info was entered if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(description) || string.IsNullOrEmpty(tagText)) { throw new Exception("Please enter a title, description, and some tags to describe the map."); } // Call a function to save the map as a new portal item await SaveNewMapAsync(MyMapView.Map, title, description, tagText.Split(','), thumbnailImg); // Report a successful save var messageDialog = new MessageDialog("Saved '" + title + "' to ArcGIS Online!", "Map Saved"); await messageDialog.ShowAsync(); } else { // This is not the initial save, call SaveAsync to save changes to the existing portal item await myMap.SaveAsync(); // Get the file stream from the new thumbnail image Stream imageStream = await thumbnailImg.GetEncodedBufferAsync(); // Update the item thumbnail (myMap.Item as PortalItem).SetThumbnailWithImage(imageStream); await myMap.SaveAsync(); // Report update was successful var messageDialog = new MessageDialog("Saved changes to '" + myMap.Item.Title + "'", "Updates Saved"); await messageDialog.ShowAsync(); } } catch (Exception ex) { // Report error message var messageDialog = new MessageDialog("Error saving map to ArcGIS Online: " + ex.Message); await messageDialog.ShowAsync(); } finally { // Hide the progress bar SaveProgressBar.Visibility = Visibility.Collapsed; } }