【问题标题】:How to copy the selected image from picture library to the images folder dynamically in the application?如何在应用程序中将所选图片从图片库动态复制到图片文件夹中?
【发布时间】:2010-11-23 06:46:09
【问题描述】:

我是 windows phone 7 应用程序开发的新手。我正在使用 PhotoChooserTask 类访问图片库。从图片库中选择一张图片后,我想将该图片(.jpg 文件)从图片库添加到我的应用程序的图像文件夹中。这该怎么做 ?我正在使用以下代码

public partial class MainPage : PhoneApplicationPage
    {

        PhotoChooserTask photoChooserTask; 
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed); 
         }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            photoChooserTask.Show();            
        }

        void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BitmapImage bmp = new BitmapImage();
                bmp.SetSource(e.ChosenPhoto);

            }
        } 
    }

我想将所选图像动态添加到我的应用程序的图像文件夹中。这该怎么做?您能否提供我可以解决上述问题的任何代码或链接?

【问题讨论】:

    标签: c# silverlight windows-phone-7


    【解决方案1】:

    下面是一个例子,将选中的图片保存到IsolatedStorage,然后读出来显示在页面上:

    void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            var contents = new byte[1024];
    
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var local = new IsolatedStorageFileStream("image.jpg", FileMode.Create, store))
                {
                    int bytes;
                    while ((bytes = e.ChosenPhoto.Read(contents, 0, contents.Length)) > 0)
                    {
                        local.Write(contents, 0, bytes);
                    }
                }
    
                // Read the saved image back out
                var fileStream = store.OpenFile("image.jpg", FileMode.Open, FileAccess.Read);
                var imageAsBitmap = PictureDecoder.DecodeJpeg(fileStream);
    
                // Display the read image in a control on the page called 'MyImage'
                MyImage.Source = imageAsBitmap;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以参考修订后的照片选择器 API 上的 RTM release notes 或此处页面下方的文档。

      How to: Create a Photo Extras Application for Windows Phone

      【讨论】:

        【解决方案3】:

        其实,一旦你获得了流,你就可以将其转换为字节并存储在本地。这是您的 Task_Completed 事件处理程序中应该包含的内容:

        using (MemoryStream stream = new MemoryStream())
        {
            byte[] contents = new byte[1024];
            int bytes;
        
            while ((bytes = e.ChosenPhoto.Read(contents, 0, contents.Length)) > 0)
            {
                stream.Write(contents, 0, bytes);
            }
        
            using (var local = new IsolatedStorageFileStream("myImage.jpg", FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication()))
            {
                local.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
            }
        }
        

        【讨论】:

        • 仅供参考。您无需创建单独的 MemoryStream 即可执行此操作。另外,您的代码不会处理 GetUserStoreForApplication() 返回的 IsolatedStorageFile。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-13
        • 2016-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多