4

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

平台-> 使用 c# 的 windows 8 Metro 应用程序

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

在使用 c# 的 Windows 8 Metro 应用程序中。我需要保存或存储(在设备中)我在运行时创建的缩略图。在 constants.cs 类文件的 DisplayResult() 中,我需要将该图像保存在设备中如何实现这一点。请给我一些想法或示例,我是移动领域的新手,从未使用过图像和缩略图部分。提前致谢 。

4

1 回答 1

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;
    }
}
于 2013-09-17T08:00:33.157 回答