// 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();
                }
            }
        }
Пример #2
0
        // 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
            }
        }