如何在Windows 8 Metro应用程序中的设备中存储保存缩略图图像C# [英] How to store save Thumbnail image in device in windows 8 metro apps c#
问题描述
我正在创建Thumbnail,并使用此代码在框架中显示
I am creating Thumbnail and showing in frame by using this code
平台->使用c#的Windows 8 Metro应用程序
Platform -> windows 8 metro apps using c#
http://code.msdn.microsoft.com/windowsapps/文件和文件夹缩略图-1d530e5d
在Windows 8 Metro应用程序中使用c#.我需要保存或存储(在设备中)运行时创建的缩略图.在constants.cs类文件的DisplayResult()中,我需要将该图像保存在设备中,以实现此目的.请给我一些想法或示例,我是移动设备的新手,从未在Image and thumbnails Part上工作过.预先感谢.
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 .
推荐答案
尝试一下.下面的代码将选择的音频文件的专辑封面保存在TempFolder中
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);
}
}
}
}
}
更新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;
}
}
这篇关于如何在Windows 8 Metro应用程序中的设备中存储保存缩略图图像C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!