如何将图像从PhotoChooserTask复制到一个位图(How to copy an image

2019-09-21 07:57发布

我一直在试图让用户从PhotoChooserTask选择,并以此作为背景图像在我的申请hubtile照片的副本,但我不能找出properr的方式来做到这一点。 截至目前,我可以设置hubtile图像到PhotoChooserTask_Completed反应,但我不知道如何执行复制操作的位图,这样我可以保存该图像时,应用程序再次启动或重新进行。 到目前为止,我有如下:

我创建一个自定义TileItem类来管理数据,我HubTiles

TileItem.cs

public class TileItem
{
    //Old Implementation        
    //public string ImageUri  { get; set; }

    //New Implementation
    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; }

MainPage.xaml.cs中

PhotoChooserTask photoChooserTask;
TileItem tileItem;

public MainPage()
    {
        InitializeComponent();

        CreateHubTiles();

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

接下来,我创建切片和使用自定义设置类坚持我的数据(即“ImageUri”现在是一个位图)

public void CreateHubTiles()
    {            
        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 = Settings.linkImage.Value, Title = "link", /*Notification = "last shared status message",*/ Message = Settings.linkUri.Value, GroupTag = "TileGroup", TileName = AppResource.Main_MainPage_HubTile_Link_Title },
        };

        this.tileList.ItemsSource = tileItems;
    }

然后我照顾PhotoChooserTask处理程序

public void changePictureMenuItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        var menuItem = (MenuItem)sender;
        tileItem = menuItem.DataContext as TileItem;  //used in 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)
    {

        if (e.TaskResult == TaskResult.OK)
        {
            //BitmapImage bitmap = new BitmapImage();
            //bitmap.SetSource(e.ChosenPhoto);

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

            switch (tileTitle)
            {
                case "status":

                    tileItem.ImageUri.SetSource(e.ChosenPhoto);  //changes the hubtile image to selected image
                    //Settings.shareImage.Value.Equals(e.ChosenPhoto);

                    //Set the selected image to the shareImage bitmap in the Settings class to persist image
                    Settings.shareImage.Value.Equals(tileItem.ImageUri); //not working!                      
                    break;
                case "link":
                    //Same as above
                    tileItem.ImageUri.SetSource(e.ChosenPhoto);
                    //Settings.linkImage.Value.Equals(e.ChosenPhoto);
                    Settings.linkImage.Value.Equals(tileItem.ImageUri);
                    break;
            }

        }
        else if (e.TaskResult == TaskResult.Cancel)
            //MessageBox.Show("No picture was chosen - operation was cancelled", "Picture not chosen", MessageBoxButton.OK);
            MessageBox.Show(AppResource.Main_MainPage_ContextMenu_ChangePicture_TaskResultCancel_Message, AppResource.Main_MainPage_ContextMenu_ChangePicture_TaskResultCancel_Caption, MessageBoxButton.OK);
        else
            //MessageBox.Show("Error while choosing photo:\n" + e.Error.Message, "Fail", MessageBoxButton.OK);
            //MessageBox.Show("Error while choosing picture", "Fail", MessageBoxButton.OK);
            MessageBox.Show(AppResource.Main_MainPage_ContextMenu_ChangePicture_TaskResultError_Message, AppResource.Main_MainPage_ContextMenu_ChangePicture_TaskResultError_Caption, MessageBoxButton.OK);
    }

本来Settings.shareImage.ValueSettings.linkImage.value设置,但在PhotoChooserTask_Completed事件不会更改或更新相应。

Settings.cs

public static readonly Setting<BitmapImage> shareImage = new Setting<BitmapImage>("shareImage", new BitmapImage(new Uri("/Images/shareStatusImage.jpg", UriKind.Relative)));

public static readonly Setting<BitmapImage> linkImage = new Setting<BitmapImage>("linkImage", new BitmapImage(new Uri("/Images/shareLinkImage.jpg", UriKind.Relative)));

因此,图像被更新仅(在PhotoChooserTask_Completed事件),而该应用没有关闭,并且然后在重新发起或激活它们被设置回orginial图像。 我怎样才能据此coppy这些图像,以便从PhotoChooserTask所选择的图像然后可以永久使用作为新ImageUri (这是现在实际上根据我的第一类中的位图)每个TileItem?

Answer 1:

如果我理解你corectly你需要找到一个方法来拍摄的图像保存到独立存储和负载在下次启动的应用程序。 看看http://www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage---Read-and-Save-Images ,它包含了完整的代码保存和加载一个位图图像



文章来源: How to copy an image from PhotoChooserTask to a Bitmap