private async void Crop_Click(object sender, RoutedEventArgs e) { #region File Setup // Load a File Picker that shows image file types FileOpenPicker open = new FileOpenPicker(); open.FileTypeFilter.Add(".jpg"); open.FileTypeFilter.Add(".png"); open.FileTypeFilter.Add(".jpeg"); open.ViewMode = PickerViewMode.Thumbnail; open.SuggestedStartLocation = PickerLocationId.PicturesLibrary; open.CommitButtonText = "Open"; // Wait for user to select a file StorageFile source = await open.PickSingleFileAsync(); // Verify the source is not null if (source != null) { try { // Create a destination file StorageFile dest = await KnownFolders.PicturesLibrary.CreateFileAsync("Cropped.jpg", CreationCollisionOption.ReplaceExisting); #endregion // Call CropImageAsync and receive Result LaunchUriResult result = await this.CropImageAsync(source, dest, 500, 500); // Load Destination Image into Image Preview var stream = await dest.OpenReadAsync(); await AppData.currentImage.SetSourceAsync(stream); #region Error Handling // Verify result and load picture into the source if (result.Status == LaunchUriStatus.Success && result.Result != null) { string imgstr = await ImageTools.FileToString(dest); if (imgstr != null) { await AppData.currentImage.SetSourceAsync(ImageTools.DecodeStringToBitmapSource(imgstr)); AppData.currentImageString = imgstr; } } } catch { MessageDialog md = new MessageDialog("Error loading image file."); await md.ShowAsync(); } } #endregion }
private async void Grayscale_Click(object sender, RoutedEventArgs e) { using (var connection = new AppServiceConnection()) { connection.AppServiceName = "com.microsoft.grayscaleservice"; connection.PackageFamilyName = "AppExtensibility.Extension.Grayscale_byq669axdz8jy"; AppServiceConnectionStatus status = await connection.OpenAsync(); if (status == AppServiceConnectionStatus.Success) { #region SendMessage // send request to service var request = new ValueSet(); request.Add("Command", "Grayscale"); request.Add("Pixels", ImageTools.GetBitmapBytes(AppData.currentImage)); request.Add("Height", AppData.currentImage.PixelHeight); request.Add("Width", AppData.currentImage.PixelWidth); // get response AppServiceResponse response = await connection.SendMessageAsync(request); if (response.Status == AppServiceResponseStatus.Success) #endregion #region HandleMessage { #region ErrorHandling // convert imagestring back ValueSet message = response.Message as ValueSet; if (message.ContainsKey("Pixels") && message.ContainsKey("Height") && message.ContainsKey("Width")) { #endregion byte[] pixels = message["Pixels"] as byte[]; int height = (int)message["Height"]; int width = (int)message["Width"]; // encode the bytes to a string, and then the image. string encodedImage = await ImageTools.EncodeBytesToPNGString(pixels, (uint)width, (uint)height); await AppData.currentImage.SetSourceAsync(ImageTools.DecodeStringToBitmapSource(encodedImage)); AppData.currentImageString = encodedImage; } } #endregion } } }
// called when the javascript in the extension signals a notify // we use this to receive image data from the callback private async void ExtensionCallback(object sender, NotifyEventArgs e) { if (this._loaded) { try { string encodedImage = ImageTools.StripDataURIHeader(e.Value); await AppData.currentImage.SetSourceAsync(ImageTools.DecodeStringToBitmapSource(encodedImage)); AppData.currentImageString = encodedImage; } catch (Exception ex) { return; } } }
// open image button private async void Open_Click(object sender, RoutedEventArgs e) { // open file FileOpenPicker open = new FileOpenPicker(); open.FileTypeFilter.Add(".jpg"); open.FileTypeFilter.Add(".png"); open.FileTypeFilter.Add(".jpeg"); open.ViewMode = PickerViewMode.Thumbnail; open.SuggestedStartLocation = PickerLocationId.PicturesLibrary; open.CommitButtonText = "Open"; // Open a stream for the selected file StorageFile file = await open.PickSingleFileAsync(); // load the file as the image if (file != null) { try { string imgstr = await ImageTools.FileToString(file); if (imgstr != null) { await AppData.currentImage.SetSourceAsync(ImageTools.DecodeStringToBitmapSource(imgstr)); AppData.currentImageString = imgstr; } } catch { MessageDialog md = new MessageDialog("Error loading image file."); await md.ShowAsync(); } } }
// this calls the 'extensionLoad' function in the script file, if it exists. public async void InvokeLoad(string str) { if (this._loaded) { if (_serviceName == null) { try { // this is dangerous! await _extwebview.InvokeScriptAsync("extensionLoad", new string[] { str }); } catch (Exception e) { // show errors return; } } #region App Service // App services are a better approach! else { try { // do app service call using (var connection = new AppServiceConnection()) { // service name was in properties connection.AppServiceName = _serviceName; // package Family Name is in the extension connection.PackageFamilyName = _extension.Package.Id.FamilyName; // open connection AppServiceConnectionStatus status = await connection.OpenAsync(); if (status != AppServiceConnectionStatus.Success) { Debug.WriteLine("Failed App Service Connection"); } else { // send request to service var request = new ValueSet(); request.Add("Command", "Load"); request.Add("Pixels", ImageTools.GetBitmapBytes(AppData.currentImage)); request.Add("Height", AppData.currentImage.PixelHeight); request.Add("Width", AppData.currentImage.PixelWidth); // get response AppServiceResponse response = await connection.SendMessageAsync(request); if (response.Status == AppServiceResponseStatus.Success) { ValueSet message = response.Message as ValueSet; if (message.ContainsKey("Pixels") && message.ContainsKey("Height") && message.ContainsKey("Width")) { byte[] pixels = message["Pixels"] as byte[]; int height = (int)message["Height"]; int width = (int)message["Width"]; #region Set Image to the new pixels // encode the bytes to a string, and then the image // this is for interop with the js extensions // wouldn't be needed if all extensions were implemented as app services string encodedImage = await ImageTools.EncodeBytesToPNGString(pixels, (uint)width, (uint)height); await AppData.currentImage.SetSourceAsync(ImageTools.DecodeStringToBitmapSource(encodedImage)); AppData.currentImageString = encodedImage; #endregion } } } } } catch (Exception e) { MessageDialog md = new MessageDialog("Invoking app service failed!"); await md.ShowAsync(); } } #endregion } }