Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store save Thumbnail image in device in windows 8 metro apps c#

I am creating Thumbnail and showing in frame by using this code

Platform -> windows 8 metro apps using c#

http://code.msdn.microsoft.com/windowsapps/File-and-folder-thumbnail-1d530e5d

in windows 8 metro apps using c#. i need to save or Store ( in device )the thumbnail image which i am creating at run time. in DisplayResult() of constants.cs class file i need to save that image in device how to achieve this . please give me some idea or example i am very new in mobile and never worked on Image and thumbnails Part . Thanks in advance .

like image 227
user2716989 Avatar asked Feb 16 '23 06:02

user2716989


1 Answers

Try this. The below code will save picked audio file's album art in TempFolder

private async void btnPickFile_Click(object sender, RoutedEventArgs e)
{
    string[] Music = new string[] { ".mp3", ".wma", ".m4a", ".aac" };
    FileOpenPicker openPicker = new FileOpenPicker();
    foreach (string extension in Music)
    {
        openPicker.FileTypeFilter.Add(extension);
    }

    StorageFile file = await openPicker.PickSingleFileAsync();
    if (file != null)
    {
        await SaveThumbnail("MySongThumb.png", file);
    }
}

private async Task SaveThumbnail(string ThumbnailName, StorageFile file)
{
    if (file != null)
    {
        using (StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(ThumbnailMode.MusicView, 100))
        {
            if (thumbnail != null && thumbnail.Type == ThumbnailType.Image)
            {
                var destinationFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(ThumbnailName, CreationCollisionOption.GenerateUniqueName);
                Windows.Storage.Streams.Buffer MyBuffer = new Windows.Storage.Streams.Buffer(Convert.ToUInt32(thumbnail.Size));
                IBuffer iBuf = await thumbnail.ReadAsync(MyBuffer, MyBuffer.Capacity, InputStreamOptions.None);
                using (var strm = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await strm.WriteAsync(iBuf);
                }
            }
        }
    }
}

UPDATE 1

private async Task<StorageFile> SaveThumbnail(StorageItemThumbnail objThumbnail)
{
    if (objThumbnail != null && objThumbnail.Type == ThumbnailType.Image)
    {
        var picker = new FileSavePicker();
        picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        picker.FileTypeChoices.Add("JPEG Image", new string[] { ".jpg" });
        picker.FileTypeChoices.Add("PNG Image", new string[] { ".png" });
        StorageFile destinationFile = await picker.PickSaveFileAsync();

        if (destinationFile != null)
        {
            Windows.Storage.Streams.Buffer MyBuffer = new Windows.Storage.Streams.Buffer(Convert.ToUInt32(objThumbnail.Size));
            IBuffer iBuf = await objThumbnail.ReadAsync(MyBuffer, MyBuffer.Capacity, InputStreamOptions.None);
            using (var strm = await destinationFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                await strm.WriteAsync(iBuf);
            }
        }

        return destinationFile;
    }
    else
    {
        return null;
    }
}
like image 155
Farhan Ghumra Avatar answered Apr 28 '23 04:04

Farhan Ghumra