开发项目时,有时需要将本地媒体文件的缩略图保存到本地,下面是源码。

需要打开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 保存音乐或视频缩略图图片到本地的更多相关文章

  1. php表单提交 图片、音乐、视频、文字,四种类型共同提交到数据库

    这个问题一直困扰了我好几天,终于在今天让我给解决了,难以掩饰的激动. 其实在之前没有接触到这种问题,只是表单提交数据而已,再就是图片,四种类型同时提交还真是没遇到过,做了一个系统,其中有一个功能就是提 ...

  2. HTML5:图片、音乐和视频

    图片.音乐和视频 一.图片 1.属性 属性 说明 alt 规定图像的替代文本. src 规定显示图像的 URL align 规定如何根据周围的文本来排列图像. border 定义图像周围的边框. he ...

  3. 三大视频网站Url的处理保存(视频和图片二选一操作)

    前台Js // 视频处理 var textVideoLink=$("input[name='textVideoLink']").val(); // 去除所有有的引号和空格 var ...

  4. 如何使用Camtasia给视频或者图片调色

    喜欢摄影过着做视频的朋友一定知道,一张好看的照片或者一段精美视频的构成因素很多,取景本身肯定是个很重要的条件,相机的素质是非常重要的硬件条件,接下来的就是后期的编辑和处理了,而在后期处理过程中调色就显 ...

  5. 使用Camtasia来给视频或者图片调色

    喜欢摄影和制作视频的朋友可能知道,一张好看的照片或一段精美视频的构成要素很多,取景本身就是很重要的条件,相机的硬件水平也是一个重要因素,接下来的就是后期的编辑和处理了,而在后期处理过程中调色是十分重要 ...

  6. 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器,适配了iOS6-9系统,3行代码即可集成.

    提示:如果你发现了Bug,请尝试更新到最新版.目前最新版是1.6.4,此前的版本或多或少存在一些bug的~如果你已经是最新版了,请留一条评论,我看到了会尽快处理和修复哈~ 关于升级iOS10和Xcdo ...

  7. 使用AVFoundation仅仅生成缩略图,不进行播放视频(本地和网络文件都可以创建视频缩略图)

    使用MPMoviePlayerController来生成缩略图足够简单,但是如果仅仅是是为了生成缩略图而不进行视频播放的话,此刻使用 MPMoviePlayerController就有点大材小用了.其 ...

  8. 利用FFmpeg生成视频缩略图 2.1.6

    利用FFmpeg生成视频缩略图 1.下载FFmpeg文件包,解压包里的\bin\下的文件解压到 D:\ffmpeg\ 目录下. 下载地址 http://ffmpeg.zeranoe.com/build ...

  9. iOS截取视频缩略图的两种方法

    前言: 看完了使用MPMoviePlayerController播放在线视频,在实际应用中有时候须要获取视频的缩略图,我们来看看怎样截取指定时间内的视频缩略图. 一  使用MPMoviePlayerC ...

随机推荐

  1. Asteroids POJ - 3041 【最小点覆盖集】

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  2. 【题解】狼和羊-C++

    Description 米基家的后院养着一群羊,米基由于疲劳睡着了,这时一群饿狼钻进了后院开始攻击羊群,后院是由许多个方格构成的长方形区域,每个方格中用字符'?'表示空地,'#'表示栅栏,'o'表示羊 ...

  3. 7.26T1四分图匹配

    四分图匹配 题目描述 一天晚上,zzh 在做梦,忽然梦见了她. 见到她,zzh 也不去看她,只顾低头自语…… “噫,OI 这个东西,真是无奇不有.” “嘿,你又学了什么?” “嗯,学到了一种算法,”z ...

  4. Geos判断点是否在多边形内

    使用的geo版本是3.5.1 #include <iostream> #include "geos.h" using namespace std; GeometryFa ...

  5. mysql 运行中 偶尔 报错 2002 也许是这个问题,内存不足导致的

    配置都是正常,最终发现内存使用率高达90%多,加配置后解决问题(升级服务器配置) 加之前是2核4g ,升级配置后 4核8g

  6. 八十年代的经典 NFS和AFS

    NFS:(C/S模式) 大体架构: 1985年Sun公司基于UDP开发了网络共享文件系统(Network FileSystem) NFS由一系列NFS命令和进程组成的客户机/服务器模式,在第三版中加入 ...

  7. 2019PKUWC游记

    有的时候,不是你不会 而是你,认为你不会 ——*Miracle* 本篇游记就简单写了 Day-inf 犹豫许久,还是选择了北大 不是因为喜欢——甚至恰好相反 而是,听说清华高手较多,约型单一, 于是我 ...

  8. 解决python中文乱码的方法

    首先需要说明的是,windows下的文件路径,cmd窗口等默认编码都是gbk 但在windows下编写python程序的时候,我们一般采用的编码是utf-8 二者不一致是导致乱码的根本原因! 在pyc ...

  9. JAVA基础知识|堆和栈

    一.java六个存储数据的地方 1)寄存器(register):这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据需求进行分配.你不能直 ...

  10. QtCreator集成的MSVC套件有问题

    MSVC编译出来的内部签名算法的程序,相同的代码,验签结果和MINGW编译出来的不一样.MINGW编译出来的结果是正确的 怀疑是因为QtCreator集成的msvc有问题,可能是编码问题,可能是其他问 ...