none
saving a simple image

    Question

  • I am trying to load an image as shown below:

     Dim openPicker As New FileOpenPicker
            openPicker.ViewMode = PickerViewMode.Thumbnail
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary
            openPicker.FileTypeFilter.Add(".jpg")
            openPicker.FileTypeFilter.Add(".jpeg")
            openPicker.FileTypeFilter.Add(".png")
            Dim file As StorageFile = Await openPicker.PickSingleFileAsync
            If Nothing IsNot file Then
                Dim image As New BitmapImage()
                Dim stream = Await file.OpenAsync(Windows.Storage.FileAccessMode.Read)
                image.SetSource(stream)
                Image1.Source = image
                LayoutRoot.Visibility = Windows.UI.Xaml.Visibility.Collapsed
                txtImgdisplay.Text = file.Path
              Else
                txtImgdisplay.Text = "Invalid File"
            End If

    And Now I have a button where I need to save the Image in that Image control to PicturesLibrary folder.

    I am trying to do as shown below but it saves an empty Image:

     Dim fileSavePicker As New FileSavePicker()
            fileSavePicker.FileTypeChoices.Add("PNG", New String() {".png"})
            fileSavePicker.FileTypeChoices.Add("JPG", New String() {".jpg"})
            fileSavePicker.FileTypeChoices.Add("BMP", New String() {".bmp"})
            fileSavePicker.FileTypeChoices.Add("TIFF", New String() {".tiff"})
            fileSavePicker.FileTypeChoices.Add("EXIF", New String() {".exif"})
            fileSavePicker.FileTypeChoices.Add("ICO", New String() {".ico"})
            Dim saveFile As StorageFile = Await fileSavePicker.PickSaveFileAsync()
    
            If Nothing IsNot saveFile Then
                Dim image As New BitmapImage()
                Dim stream = Await StorageFile.GetFileFromPathAsync(txtImgdisplay.Text)
                LayoutRoot.Visibility = Windows.UI.Xaml.Visibility.Collapsed
                txtImgdisplay.Text = saveFile.Path
                Image1.Source = image
                Dim copyFile As StorageFile = Await saveFile.CopyAsync(Windows.Storage.KnownFolders.PicturesLibrary, "sample - Copy.png")
            Else
                txtImgdisplay.Text = "Invalid File"
            End If

    So how can I do that?


    kiran.


    Thursday, April 05, 2012 8:24 PM

All replies

  • Kiran,

    Didn't you intend to do:

    file.CopyAndReplaceAsync(saveFile);

     file is the one that you picked with FileOpenPicker.

    - ngm

    Thursday, April 05, 2012 11:23 PM
  • Hi ngm011,

    I have tried that and worked fine when it is inside button1_click but I need it within button2_click event.


    kiran.

    Thursday, April 05, 2012 11:54 PM
  • You will need to remember the file object that you want to copy so you can use it later. Instead of storing the "file" object returned from the FileOpenPicker as a local variable in button1_click, store it in a class variable so you can still use it in button2_click.

    You can't read the pixels of the BitmapImage to save them out, so you need to remember what they were before they went in (or use a WriteableBitmap, but that is more complex and unnecessary if you don't need to change the pixels). The easiest way is to cache the StorageFile that was returned from the FileOpenPicker.

    --Rob

    Friday, April 06, 2012 12:05 AM
  • Hi Rob,

    Thanks for the explanation.But Still I am in dilemma how to use that can you show me some sample for that?

    I need similarly to this sample.


    kiran.



    Friday, April 06, 2012 8:25 AM