【问题标题】:How to store save Thumbnail image in device in windows 8 metro apps c#如何在 Windows 8 Metro 应用程序中将保存的缩略图图像存储在设备中 c#
【发布时间】:2013-09-17 07:03:45
【问题描述】:

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

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

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

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

【问题讨论】:

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


    【解决方案1】:

    试试这个。下面的代码会将选取的音频文件的专辑封面保存在 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;
        }
    }
    

    【讨论】:

    • thanx xyroid ,您的代码帮助很大,但问题是我不需要再次选择文件,因为我已经创建了之前的缩略图,我尝试在不使用 openPicker 的情况下修改您的代码,但没有得到成功在那。如果 StorageFile 和 savePicker 而不是 openPicker 对象,我们可以使用 StorageFolder 吗?我还想提供将缩略图保存到我通过 savePicker 选择的位置的位置。我查了很多东西才找到这样的选项,但没有用。
    • 所以您已经拥有StorageItemThumbnail 对象并希望通过SavePicker 保存它?
    • 对,你是对的。我已经有 StorageItemThumbnail 对象,并希望将其保存在设备中。
    • 太好了,非常感谢您的回复。这非常有用。实际上,我想将这些缩略图存储在我的 c#/xaml Metro 应用程序中的 sqlite 数据库中。我请求您让我知道如何实现这一点,期待您的回复....
    • 您可以将其存储为 SQLite 中的字节数组。您也可以将图像保存在本地应用程序文件夹中,并将文件名保存在数据库中。
    猜你喜欢
    • 2012-08-26
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    相关资源
    最近更新 更多