locked
Save DataPackageView.Properties.Thumbnail to disk

    Question

  • How to save DataPackageView.Properties.Thumbnail to disk as .jpg file?
    • Edited by ivan.icin Monday, March 31, 2014 10:46 PM
    Monday, March 31, 2014 8:24 PM

Answers

  • Hi,

    You can use FileSavePicker to save the file to jpg file. And the StorageFile.CreateStreamedFileAsync can create a typed file base with the displayNameWithExtension. Some codes below:

    private async void Button_Click(object sender, RoutedEventArgs e)
            {
                var fileBytes = new byte[200]; 
                var streamReference =dataPackageView.Properties.Thumbnail;
                // Creating the streamed storage file.
                var streamedStorageFile = await StorageFile.CreateStreamedFileAsync("MyFileName.jpg", async stream =>
                {
                    await stream.WriteAsync(fileBytes.AsBuffer());
                    await stream.FlushAsync();
                    stream.Dispose();
                }, streamReference);
                
                FileSavePicker picker = new FileSavePicker();
                picker.DefaultFileExtension = ".jpg";
                picker.FileTypeChoices.Add("JPEG Image", new string[] { ".jpg" });
                var target = await picker.PickSaveFileAsync();
                await streamedStorageFile.CopyAndReplaceAsync(target);
            }

    Please refer to the link below:

    http://socialeboladev.wordpress.com/2013/03/15/how-to-use-createstreamedfileasync/

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, April 03, 2014 3:16 AM

All replies

  • Hi,

    Try some codes below:

    // Lets say the byte array represents the data we want to present as a storage file.
    var fileBytes = new byte[200];var streamReference =DataPackageView.Properties.Thumbnail; // Creating the streamed storage file. var streamedStorageFile = await StorageFile.CreateStreamedFileAsync("MyFileName.jpg", async stream => { await stream.WriteAsync(fileBytes.AsBuffer()); await stream.FlushAsync(); stream.Dispose(); }, streamReference);

    Please refer to the link get more information:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.createstreamedfileasync.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Dave SmitsMVP Tuesday, April 01, 2014 10:30 AM
    Tuesday, April 01, 2014 2:45 AM
  • This is a partial answer, though obviously on a very good track.

    OK, it is easy guess that I need to copy streamedStorageFile to StorageFile to get a real file (though that should have been part of the answer), but what about encoding to .jpg?


    • Edited by ivan.icin Tuesday, April 01, 2014 6:53 PM grammar error
    Tuesday, April 01, 2014 6:48 PM
  • Hi,

    You can use FileSavePicker to save the file to jpg file. And the StorageFile.CreateStreamedFileAsync can create a typed file base with the displayNameWithExtension. Some codes below:

    private async void Button_Click(object sender, RoutedEventArgs e)
            {
                var fileBytes = new byte[200]; 
                var streamReference =dataPackageView.Properties.Thumbnail;
                // Creating the streamed storage file.
                var streamedStorageFile = await StorageFile.CreateStreamedFileAsync("MyFileName.jpg", async stream =>
                {
                    await stream.WriteAsync(fileBytes.AsBuffer());
                    await stream.FlushAsync();
                    stream.Dispose();
                }, streamReference);
                
                FileSavePicker picker = new FileSavePicker();
                picker.DefaultFileExtension = ".jpg";
                picker.FileTypeChoices.Add("JPEG Image", new string[] { ".jpg" });
                var target = await picker.PickSaveFileAsync();
                await streamedStorageFile.CopyAndReplaceAsync(target);
            }

    Please refer to the link below:

    http://socialeboladev.wordpress.com/2013/03/15/how-to-use-createstreamedfileasync/

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, April 03, 2014 3:16 AM