5

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 .

Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115
user2716989
  • 53
  • 1
  • 4

1 Answers1

10

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;
    }
}
Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115
  • thanx xyroid , Your code helped a lot but the thing is that i need not to select the file again because i have created thumbnail of that earlier i tried to modify your code without using openPicker but did not get success in that . can we use StorageFolder instead if StorageFile and savePicker instead of openPicker object , i also want to give the location to save that tumbnail to that location which i have selected by savePicker . I chked a lot to find such option but did not get useful . – user2716989 Sep 17 '13 at 12:37
  • So you have `StorageItemThumbnail` object already and want to save it via `SavePicker` ? – Farhan Ghumra Sep 17 '13 at 13:00
  • right you are right . i have StorageItemThumbnail object already and want to save this in device . – user2716989 Sep 18 '13 at 05:39
  • That was Great , thanks a lot for the reply . It was very useful. actually i want to store these tumbnails in sqlite database in my c#/xaml metro app., i request you to please let me know how can i achieve this , looking forward for your response.... – user2716989 Sep 18 '13 at 06:31
  • 1
    You can store it as byte array in SQLite. You can also save the images in Local app folder and save the file name in database. – Farhan Ghumra Sep 18 '13 at 06:47