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

泄露秘密 提交于 2019-12-06 04:21:40

问题


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 .


回答1:


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;
    }
}


来源:https://stackoverflow.com/questions/18843315/how-to-store-save-thumbnail-image-in-device-in-windows-8-metro-apps-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!