0

I am trying to save an image selected with FileOpenPicker. I am lunching this event when an image is selected

 async void  photoChooserTask_Completed(object sender, PhotoResult e)
{       
// get the file stream and file name           
                Stream photoStream = e.ChosenPhoto;    
                string fileName = Path.GetFileName(e.OriginalFileName);

  // persist data into isolated storage      
    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);    
    using (Stream current = await file.OpenStreamForWriteAsync())    
    {     
        await photoStream.CopyToAsync(current);         
    }      
}         

But this code which will give me the lenght of the saved file return 0

var properties = await file.GetBasicPropertiesAsync();
                i = properties.Size;

Have I done something wrong in saving the image?

AnotherGeek
  • 874
  • 1
  • 6
  • 24
  • Check http://stackoverflow.com/questions/19886950/save-the-image-in-storage-file-in-windows-phone-8 this is basically same. – loop May 20 '14 at 09:47
  • Saw it but didn't resolve my problem – AnotherGeek May 20 '14 at 09:52
  • Hi, You are not doing anything wrong everything is working fine i have tested it. may be problem is different or you are doing a mistake in checking it. plz crosscheck it.put a break point on next line of "i = properties.size". – loop May 20 '14 at 10:11
  • Update your question and add the setting image.source code make it as it is initial..then it will help other people. – loop May 20 '14 at 15:46

1 Answers1

2

You may need to flush the stream.

If that does not work, add a breakpoint and check the two streams' lengths after the copy. Are they equal? They should be. Anything suspicious on those two stream objects?

Edit

On the image you posted, I can see that you use the SetSource method of a BitmapImage with the same Stream that you copy. Once you do that, the Stream's Position will be at the end, as it was just read by that call.

CopyToAsync copies everything after the current Position of the Stream you call it on. Since the position is at the end, because it was just read, the CopyToAsync does not copy anthing.

All you need to do to fix your problem is set the stream's Position to 0.

yasen
  • 3,580
  • 13
  • 25