保存图片到手机图片库

最近因为项目需要,需将图片保存至手机图片库,以前都只是将图片存放在独立存储中,刚好学习了一下~

前台只是一个简单的图片控件,保存按钮,然后再一按钮用于查看我们是否将图片保存到手机图片库中~

<Image Height="395"
                HorizontalAlignment="Left"
                Margin="6,6,0,0"
                Name="image1"
                Stretch="Fill"
                VerticalAlignment="Top"
                Width="444"
                Source="/SavePicture;component/TestImage.jpg"/>

<Button Content="Save"
                    Height="70"
                    HorizontalAlignment="Center"
                    Margin="-12,516,319,0"
                    Name="btnSave"
                    VerticalAlignment="Top"
                    Width="149" Click="btnSave_Click" />

 <Button Content="choosepic" Height="72" HorizontalAlignment="Left" Margin="206,516,0,0" Name="button1" VerticalAlignment="Top" Width="177" Click="button1_Click" />

后台

 

private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            // Create a file name for the JPEG file in isolated storage.
            String tempJPEG = "TempJPEG";

            // Create a virtual store and file stream. Check for duplicate tempJPEG files.
            var myStore = IsolatedStorageFile.GetUserStoreForApplication();
            if (myStore.FileExists(tempJPEG))
            {
                myStore.DeleteFile(tempJPEG);
            }

            IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);


            // Create a stream out of the sample JPEG file.
            // For [Application Name] in the URI, use the project name that you entered
            // in the previous steps. Also, TestImage.jpg is an example;
            // you must enter your JPEG file name if it is different.
            StreamResourceInfo sri = null;
            Uri uri = new Uri("SavePicture;component/TestImage.jpg", UriKind.Relative);
            sri = Application.GetResourceStream(uri);

            // Create a new WriteableBitmap object and set it to the JPEG stream.
            BitmapImage bitmap = new BitmapImage();
            bitmap.CreateOptions = BitmapCreateOptions.None;
            bitmap.SetSource(sri.Stream);
            WriteableBitmap wb = new WriteableBitmap(bitmap);

            // Encode the WriteableBitmap object to a JPEG stream.
            wb.SaveJpeg(myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
            myFileStream.Close();

            // Create a new stream from isolated storage, and save the JPEG file to the media library on Windows Phone.
            myFileStream = myStore.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read);

            // Save the image to the camera roll or saved pictures album.
            MediaLibrary library = new MediaLibrary();

             Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);
             MessageBox.Show("Image saved to saved pictures album");

             myFileStream.Close();

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            PhotoChooserTask task = new PhotoChooserTask();
            task.Completed += new EventHandler<PhotoResult>(task_Completed);
            task.Show();
        }

        void task_Completed(object sender, PhotoResult e)
        {
          
        }

 

 

主要实现是将图片流保存到多媒体库中

 Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);

 

posted on 2011-10-09 22:10  ShinyTang  阅读(861)  评论(2)    收藏  举报

导航

作者:LucyTangLucyTang's Blog on 博客园
出处:http://www.cnblogs.com/jing870812/

本作品由LucyTang创作,采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 任何转载必须保留完整文章,在显要地方显示署名以及原文链接。如您有任何疑问或者授权方面的协商,请给我留言