如何在Windows 8 metro应用程序中将缩略图保存在设备中c#

时间:2013-09-17 07:03:45

标签: windows-8 windows-runtime microsoft-metro winrt-xaml bitmapimage

我正在创建缩略图并使用此代码在框架中显示

平台 - >使用c#

的windows 8 metro应用程序

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

在使用c#的windows 8 metro应用程序中

。我需要保存或存储(在设备中)我在运行时创建的缩略图。在constants.cs类文件的DisplayResult()中,我需要在设备中保存该图像如何实现这一点。请给我一些想法或示例我是移动设备的新手,从未参与过图像和缩略图部分。提前谢谢。

1 个答案:

答案 0 :(得分:8)

试试这个。以下代码将在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);
                }
            }
        }
    }
}

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