如何在C#Windows Phone 8中将BitmapImage保存到StorageFolder

时间:2014-03-07 07:28:33

标签: c# windows-phone-8 bitmapimage

我正在尝试将我从网址下载的BitmapImage保存到应用StorageFolder中。 我试着制作一个为我保存图像的功能。这是我到目前为止所得到的:

public async Task<string> savePhotoLocal(BitmapImage photo, string photoName)
    {
        var profilePictures = await storageRoot.CreateFolderAsync("profilePictures", CreationCollisionOption.OpenIfExists);
        var profilePicture = await profilePictures.CreateFileAsync(photoName+".jpg", CreationCollisionOption.ReplaceExisting);

        byte[] byteArray = new byte[0];
        using (MemoryStream stream = new MemoryStream())
        {
            using (Stream outputStream = await profilePicture.OpenStreamForWriteAsync())
            {
                await stream.CopyToAsync(outputStream);
            }
        }

        return profilePicture.Path;
    }

但这不起作用,我没有收到任何错误,所以我真的不知道这里出了什么问题。任何帮助或代码示例都会很棒。

2 个答案:

答案 0 :(得分:0)

public async Task<string> savePhotoLocal(BitmapImage photo, string photoName)
    {
       string folderName ="profilePictures";
       var imageName =photoName+".jpg";        
       Stream outputStream = await profilePictures.OpenStreamForWriteAsync();
       if(outputStream!=null)
          {
           this.SaveImages(outputStream,folderName,imageName );
           }
       return imageName ;
    }





 private void SaveImages(Stream data, string directoryName, string imageName)
            {
IsolatedStorageFile StoreForApplication =IsolatedStorageFile.GetUserStoreForApplication();
                try
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        data.CopyTo(memoryStream);
                        memoryStream.Position = 0;
                        byte[] buffer = null;
                        if (memoryStream != null && memoryStream.Length > 0)
                        {
                            BinaryReader binaryReader = new BinaryReader(memoryStream);
                            buffer = binaryReader.ReadBytes((int)memoryStream.Length);
                            Stream stream = new MemoryStream();
                            stream.Write(buffer, 0, buffer.Length);
                            stream.Seek(0, SeekOrigin.Begin);
                            string FilePath = System.IO.Path.Combine(directoryName, imageName);
                            IsolatedStorageFileStream isoFileStream = new IsolatedStorageFileStream(FilePath, FileMode.Create, StoreForApplication);
                            Deployment.Current.Dispatcher.BeginInvoke(() =>
                               {
                                   BitmapImage bitmapImage = new BitmapImage { CreateOptions = BitmapCreateOptions.None };
                                   bitmapImage.SetSource(stream);
                                   WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage);
                                   writeableBitmap.SaveJpeg(isoFileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100);
                               });
                        }
                    }

                }
                catch (Exception ex)
                {
                    //ExceptionHelper.WriteLog(ex);
                }
            }

答案 1 :(得分:0)

试试这个:

通过HttpWebRequest下载图片:

 linkRequest = (HttpWebRequest)WebRequest.Create(uri);
 linkRequest.Method = "GET";
 WebRequestState webRequestState = new WebRequestState(linkRequest, additionalDataObject);
 linkRequest.BeginGetResponse(client_DownloadImageCompleted, webRequestState);

然后:

private void client_DownloadImageCompleted(IAsyncResult asynchronousResult)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            { 
                    WebRequestState webRequestState = asynchronousResult.AsyncState as WebRequestState;

                    AdditionalDataObject file = webRequestState._object;

                    using (HttpWebResponse response = (HttpWebResponse)webRequestState.Request.EndGetResponse(asynchronousResult))
                    {
                        using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                                    using (Stream stream = response.GetResponseStream())
                                    {

                                        BitmapImage b = new BitmapImage();

                                        b.SetSource(stream);
                                        WriteableBitmap wb = new WriteableBitmap(b);
                                        using (var isoFileStream = isoStore.CreateFile(yourImageFolder + file.Name))
                                        {
                                            var width = wb.PixelWidth;
                                            var height = wb.PixelHeight;
                                            System.Windows.Media.Imaging.Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
                                        }
                                    }
                        }
                 }
              });
        }

WebRequestState类是:

public class WebRequestState
    {
        public HttpWebRequest Request { get; set; }
        public object _object { get; set; }



        public WebRequestState(HttpWebRequest webRequest, object obj)
        {
            Request = webRequest;
            _object = obj;
        }
    }