C# 如何从PhotoChooserTask保存图片

C# 如何从PhotoChooserTask保存图片,c#,windows-phone-7,bitmap,C#,Windows Phone 7,Bitmap,我想保存使用PhotoChooserTask选择的照片,但我不确定正确的方法。到目前为止,我已经实现了PhotoChooserTask_Completed方法,但是将其保存到位图的正确方法是什么 编辑:添加了基本实现。目标是将Hubbile图像更新为用户从PhotoChooserTask中选择的图像 注意:我已将设置类和TileItem类放在底部以供快速参考 MainPage.xaml string shareJPEG = "shareImage.jpg"; string linkJPEG =

我想保存使用PhotoChooserTask选择的照片,但我不确定正确的方法。到目前为止,我已经实现了PhotoChooserTask_Completed方法,但是将其保存到位图的正确方法是什么

编辑:添加了基本实现。目标是将Hubbile图像更新为用户从PhotoChooserTask中选择的图像

注意:我已将设置类和TileItem类放在底部以供快速参考

MainPage.xaml

string shareJPEG = "shareImage.jpg";
string linkJPEG = "linkImage.jpg";

BitmapImage shareImg;
BitmapImage linkImg;

public MainPage()
    {
        InitializeComponent();

        CreateHubTiles();

        photoChooserTask = new PhotoChooserTask();
        photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
    }

public void CreateHubTiles()
    {     
        if (Settings.shareImageUpdated.Value == true)
        {
            //Settings.shareImage.Value = new BitmapImage();
            shareImg = new BitmapImage();

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(shareJPEG, FileMode.Open, FileAccess.Read))
                {
                    //Settings.shareImage.Value.SetSource(fileStream);
                    shareImg.SetSource(fileStream);

                    //this.img.Height = bi.PixelHeight;
                    //this.img.Width = bi.PixelWidth;

                }
            }
            //this.img.Source = bi;
        }
        else
        {
            //Settings.shareImage.Value = new BitmapImage(new Uri("/Images/shareStatusImage.jpg", UriKind.Relative));
            shareImg = new BitmapImage(new Uri("/Images/shareStatusImage.jpg", UriKind.Relative));
        }

        if (Settings.linkImageUpdated.Value == true)
        {
            //Settings.linkImage.Value = new BitmapImage();
            linkImg = new BitmapImage();

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(linkJPEG, FileMode.Open, FileAccess.Read))
                {
                    //Settings.linkImage.Value.SetSource(fileStream);
                    linkImg.SetSource(fileStream);

                    //this.img.Height = bi.PixelHeight;
                    //this.img.Width = bi.PixelWidth;

                }
            }
            //this.img.Source = bi;
        }
        else
        {
            //Settings.linkImage.Value = new BitmapImage(new Uri("/Images/shareStatusImage.jpg", UriKind.Relative));
            linkImg = new BitmapImage(new Uri("/Images/shareLinkImage.jpg", UriKind.Relative));
        }

        List<TileItem> tileItems = new List<TileItem>() 
        {                
            //new TileItem() { ImageUri = Settings.shareImage.Value, Title = "status", /*Notification = "last shared link uri",*/ Message = Settings.statusMessage.Value, GroupTag = "TileGroup", TileName = AppResource.Main_MainPage_HubTile_Status_Title },
            new TileItem() { ImageUri = shareImg, Title = "status", /*Notification = "last shared link uri",*/ Message = Settings.statusMessage.Value, GroupTag = "TileGroup", TileName = AppResource.Main_MainPage_HubTile_Status_Title },


            //new TileItem() { ImageUri = Settings.linkImage.Value, Title = "link", /*Notification = "last shared status message",*/ Message = Settings.linkUri.Value, GroupTag = "TileGroup", TileName = AppResource.Main_MainPage_HubTile_Link_Title },
            new TileItem() { ImageUri = linkImg, Title = "link", /*Notification = "last shared status message",*/ Message = Settings.linkUri.Value, GroupTag = "TileGroup", TileName = AppResource.Main_MainPage_HubTile_Link_Title },
        };

        this.tileList.ItemsSource = tileItems;
    }

public void changePictureMenuItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        var menuItem = (MenuItem)sender;
        tileItem = menuItem.DataContext as TileItem;  //for PhotoChooserTask_Completed              

        try
        {
            photoChooserTask.Show();
        }
        catch (System.InvalidOperationException ex)
        {
            //MessageBox.Show("An error occurred");
            MessageBox.Show(AppResource.Main_MainPage_ContextMenu_ChangePicture_Error_Message);
        }
    }

void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        //Debug.WriteLine("***\t In photoChooserTask_Completed function of ChoosePhotoPage\t ***");

        if (e.TaskResult == TaskResult.OK)
        {
            //get the correct hubtile that was clicked and set image source to respective hubtile
            string tileTitle = tileItem.Title.ToString();

            switch (tileTitle)
            {
                case "status":

                    tileItem.ImageUri.SetSource(e.ChosenPhoto);  //sets the tile image immediately, but does not persist when the MainPage is navigated away


                    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (myIsolatedStorage.FileExists(shareJPEG))
                        {
                            myIsolatedStorage.DeleteFile(shareJPEG);
                        }

                        IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(shareJPEG);

                        BitmapImage bitmap = new BitmapImage();
                        bitmap.SetSource(e.ChosenPhoto);
                        WriteableBitmap wb = new WriteableBitmap(bitmap);

                        // Encode WriteableBitmap object to a JPEG stream. 
                        Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);

                        //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); 
                        fileStream.Close();
                    }

                    Settings.shareImageUpdated.Value = true;  //simple boolean value that exists in isolated storage

                    break;
                case "link":
                    tileItem.ImageUri.SetSource(e.ChosenPhoto);  //sets the tile image immediately, but does not persist when the MainPage is navigated away

                    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (myIsolatedStorage.FileExists(linkJPEG))
                        {
                            myIsolatedStorage.DeleteFile(linkJPEG);
                        }

                        IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(linkJPEG);

                        BitmapImage bitmap = new BitmapImage();
                        bitmap.SetSource(e.ChosenPhoto);
                        WriteableBitmap wb = new WriteableBitmap(bitmap);

                        // Encode WriteableBitmap object to a JPEG stream. 
                        Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);

                        //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85); 
                        fileStream.Close();
                    } 

                    Settings.linkImageUpdated.Value = true;

                    break;
            }            
     }

运行此应用程序时,我没有收到任何调试错误,但位图似乎没有被正确保存或检索,因为原始磁贴图像是应用程序离开主页时唯一持续存在的图像。我在这里做错了什么?我该如何解决这个问题?

下面是将图像存储在独立存储中并将其加载回的工作代码。检查一下

string tempJPEG = "image.jpg";

    void photoTask_Completed(object sender, PhotoResult e)
    {
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsolatedStorage.FileExists(tempJPEG))
            {
                myIsolatedStorage.DeleteFile(tempJPEG);
            }

            IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);

            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(e.ChosenPhoto);
            WriteableBitmap wb = new WriteableBitmap(bitmap);

            // Encode WriteableBitmap object to a JPEG stream.
            Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);

            //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
            fileStream.Close();
        }
    }


//Code to load image from IsolatedStorage anywhere in your app
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        BitmapImage bi = new BitmapImage();

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read))
            {
                bi.SetSource(fileStream);
                this.img.Height = bi.PixelHeight;
                this.img.Width = bi.PixelWidth;
            }
        }
        this.img.Source = bi;
    }

看起来上一个问题被打断了。你的解决方案让我知道了如何纠正我的问题,但由于某种原因,我无法让它正常工作。图像没有更新吗?我按照你们的步骤编辑了我的原始问题,以准确地显示我现在拥有的内容。也许你能发现一些我没有改正的错误?
public class TileItem
{
    //public string ImageUri
    //{
    //    get;
    //    set;
    //}
    public BitmapImage ImageUri
    {
        get;
        set;
    }

    public string Title
    {
        get;
        set;
    }

    public string Notification
    {
        get;
        set;
    }

    public bool DisplayNotification
    {
        get
        {
            return !string.IsNullOrEmpty(this.Notification);
        }
    }

    public string Message
    {
        get;
        set;
    }

    public string GroupTag
    {
        get;
        set;
    }

    //for translation purposes (bound to HubTile Title on MainPage) 
    public string TileName
    {
        get;
        set;
    }
}
string tempJPEG = "image.jpg";

    void photoTask_Completed(object sender, PhotoResult e)
    {
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsolatedStorage.FileExists(tempJPEG))
            {
                myIsolatedStorage.DeleteFile(tempJPEG);
            }

            IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);

            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(e.ChosenPhoto);
            WriteableBitmap wb = new WriteableBitmap(bitmap);

            // Encode WriteableBitmap object to a JPEG stream.
            Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);

            //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
            fileStream.Close();
        }
    }


//Code to load image from IsolatedStorage anywhere in your app
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        BitmapImage bi = new BitmapImage();

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read))
            {
                bi.SetSource(fileStream);
                this.img.Height = bi.PixelHeight;
                this.img.Width = bi.PixelWidth;
            }
        }
        this.img.Source = bi;
    }