UWP 保存音乐或视频缩略图图片到本地
开发项目时,有时需要将本地媒体文件的缩略图保存到本地,下面是源码。
需要打开Package.appxmanifest 功能 图片库 访问权限。
<Page
x:Class="SaveBitmap.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SaveBitmap"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image x:Name="image"/>
<Button x:Name="filePicker_bt" Grid.Row="1" Content="FelePicker" Tapped="filePicker_bt_Tapped" HorizontalAlignment="Left"/>
<Button x:Name="savePicture_bt" Grid.Row="1" Content="SavePicture" Tapped="savePicture_bt_Tapped" HorizontalAlignment="Right"/>
</Grid>
</Page>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
// https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x804 上介绍了“空白页”项模板
namespace SaveBitmap
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private StorageFile save_file;
private async void filePicker_bt_Tapped(object sender, TappedRoutedEventArgs e)
{
FileOpenPicker file_picker = new FileOpenPicker();
file_picker.FileTypeFilter.Add(".mp4");
file_picker.FileTypeFilter.Add(".mp3");
save_file = await file_picker.PickSingleFileAsync();
try//防止专辑图片为空
{
using (var thumbnail = await save_file.GetThumbnailAsync(ThumbnailMode.MusicView, 200))//获取媒体专辑image
{
var video_cover = new BitmapImage();
video_cover.SetSource(thumbnail);
image.Source = video_cover;
}
}
catch
{
}
}
private async void savePicture_bt_Tapped(object sender, TappedRoutedEventArgs e)
{
try
{
await SaveImage(save_file, image);
bool result = await Windows.System.Launcher.LaunchFolderAsync(saved_folder);
}
catch (Exception error)
{
var message = new MessageDialog(error.ToString());
await message.ShowAsync();
}
}
private StorageFolder saved_folder;
private async Task<StorageFile> SaveImage(StorageFile file,Image image)
{
string desiredName = file.DisplayName + ".jpg";
saved_folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("Saved Picture", CreationCollisionOption.OpenIfExists);
var saveFile = await saved_folder.CreateFileAsync(desiredName, CreationCollisionOption.GenerateUniqueName);
try
{
RenderTargetBitmap bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(image);
var pixelBuffer = await bitmap.GetPixelsAsync();
using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)bitmap.PixelWidth,
(uint)bitmap.PixelHeight,
DisplayInformation.GetForCurrentView().LogicalDpi,
DisplayInformation.GetForCurrentView().LogicalDpi,
pixelBuffer.ToArray());
await encoder.FlushAsync();
}
}
catch
{
}
return saveFile;
}
}
}
运行截图

源码链接https://github.com/singhwong/Save-Bitmap-uwp.git
UWP 保存音乐或视频缩略图图片到本地的更多相关文章
- php表单提交 图片、音乐、视频、文字,四种类型共同提交到数据库
这个问题一直困扰了我好几天,终于在今天让我给解决了,难以掩饰的激动. 其实在之前没有接触到这种问题,只是表单提交数据而已,再就是图片,四种类型同时提交还真是没遇到过,做了一个系统,其中有一个功能就是提 ...
- HTML5:图片、音乐和视频
图片.音乐和视频 一.图片 1.属性 属性 说明 alt 规定图像的替代文本. src 规定显示图像的 URL align 规定如何根据周围的文本来排列图像. border 定义图像周围的边框. he ...
- 三大视频网站Url的处理保存(视频和图片二选一操作)
前台Js // 视频处理 var textVideoLink=$("input[name='textVideoLink']").val(); // 去除所有有的引号和空格 var ...
- 如何使用Camtasia给视频或者图片调色
喜欢摄影过着做视频的朋友一定知道,一张好看的照片或者一段精美视频的构成因素很多,取景本身肯定是个很重要的条件,相机的素质是非常重要的硬件条件,接下来的就是后期的编辑和处理了,而在后期处理过程中调色就显 ...
- 使用Camtasia来给视频或者图片调色
喜欢摄影和制作视频的朋友可能知道,一张好看的照片或一段精美视频的构成要素很多,取景本身就是很重要的条件,相机的硬件水平也是一个重要因素,接下来的就是后期的编辑和处理了,而在后期处理过程中调色是十分重要 ...
- 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器,适配了iOS6-9系统,3行代码即可集成.
提示:如果你发现了Bug,请尝试更新到最新版.目前最新版是1.6.4,此前的版本或多或少存在一些bug的~如果你已经是最新版了,请留一条评论,我看到了会尽快处理和修复哈~ 关于升级iOS10和Xcdo ...
- 使用AVFoundation仅仅生成缩略图,不进行播放视频(本地和网络文件都可以创建视频缩略图)
使用MPMoviePlayerController来生成缩略图足够简单,但是如果仅仅是是为了生成缩略图而不进行视频播放的话,此刻使用 MPMoviePlayerController就有点大材小用了.其 ...
- 利用FFmpeg生成视频缩略图 2.1.6
利用FFmpeg生成视频缩略图 1.下载FFmpeg文件包,解压包里的\bin\下的文件解压到 D:\ffmpeg\ 目录下. 下载地址 http://ffmpeg.zeranoe.com/build ...
- iOS截取视频缩略图的两种方法
前言: 看完了使用MPMoviePlayerController播放在线视频,在实际应用中有时候须要获取视频的缩略图,我们来看看怎样截取指定时间内的视频缩略图. 一 使用MPMoviePlayerC ...
随机推荐
- vue 内联样式style三元表达式(动态绑定样式)
<span v-bind:style="{'display':config.isHaveSearch ? 'block':'none'}" >动态绑定样式</sp ...
- focus([[data],fn]) 当元素获得焦点时,触发 focus 事件。
focus([[data],fn]) 概述 当元素获得焦点时,触发 focus 事件.直线电机选型 可以通过鼠标点击或者键盘上的TAB导航触发.这将触发所有绑定的focus函数,注意,某些对象不支持f ...
- 002_linux之点灯(汇编深度解析)
1. 开发板采用韦山东的开发板 2. 芯片CPU三星S3C2440A 3. 控制引脚:GPF4 4. linux操作系统 5. 芯片手册下载地址:https://eyun.ba ...
- 洛谷P1903 [国家集训队]数颜色 / 维护队列 ( 带 修 )
题意:有两种操作: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P Col 把第P支画笔替换为颜色Col. 对每个1操作 输出答案: 带修莫队 模板题 (加 ...
- 2D动画如何做出3D体积感
https://cowlevel.net/article/1959026 <AngerForce>幕后故事 这篇文章是个老坑,最近有时间开始写,也是对之前项目的一个总结和记录吧. 本篇文章 ...
- 1626:【例 2】Hankson 的趣味题
1626:[例 2]Hankson 的趣味题题解 [题目描述] Hanks 博士是 BT(Bio-Tech,生物技术)领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson ...
- 非旋treap
目录 核心思想 核心操作 其他操作 参考程序 核心思想 主要的思想与treap是一样的.通过让二叉查找树同时满足堆(随机参数)的性质来防止深度过大.与普通treap不同的是非旋treap通过树的分裂与 ...
- java试题复盘——11月13日
上: 10. AccessViolationException异常触发后,下列程序的输出结果为( A ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 AccessViol ...
- D. Restore Permutation(权值线段树)
D. Restore Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Robot Framework(十八) 支持工具
5支持工具 5.1库文档工具(libdoc) libdoc是一种用于为HTML和XML格式的测试库和资源文件生成关键字文档的工具.前一种格式适用于人类,后者适用于RIDE和其他工具.Libdoc也没有 ...