How to resize or create a thumbnail image from file stream on UWP
最近在搞Ocr相关的windows universal app, 用到了一些图像处理相关的知识。
涉及到了BitmapDecoder/BitmapEncoder/IRandomAccessStream等类,下面总结了IRandomAccessStream的一些扩展方法,以后还会慢慢加上其他常用的。
public static class RandomAccessStreamExtension
{
/// <summary>
/// Retrieves an adjusted thumbnail image with the specified file stream.
/// </summary>
/// <param name="inputStream">The input stream.</param>
/// <param name="requestedSize">The requested size, in pixels.</param>
/// <returns></returns>
public static async Task<byte[]> GetThumbnailAsync(this IRandomAccessStream inputStream, uint requestedSize)
{
if (inputStream == null)
return null; var decoder = await BitmapDecoder.CreateAsync(inputStream);
var originalPixelWidth = decoder.PixelWidth;
var originalPixelHeight = decoder.PixelHeight;
if (originalPixelWidth < requestedSize || originalPixelHeight < requestedSize)
{
return await inputStream.GetBytesAsync();
} using (var outputStream = new InMemoryRandomAccessStream())
{
var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder);
double widthRatio = (double)requestedSize / originalPixelWidth;
double heightRatio = (double)requestedSize / originalPixelHeight; uint aspectHeight = requestedSize;
uint aspectWidth = requestedSize; if (originalPixelWidth > originalPixelHeight)
{
aspectWidth = (uint)(heightRatio * originalPixelWidth);
}
else
{
aspectHeight = (uint)(widthRatio * originalPixelHeight);
} encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;
encoder.BitmapTransform.ScaledHeight = aspectHeight;
encoder.BitmapTransform.ScaledWidth = aspectWidth;
await encoder.FlushAsync(); return await outputStream.GetBytesAsync();
}
} /// <summary>
/// Retrieves byte array from the input stream.
/// </summary>
/// <param name="stream">The input stream.</param>
/// <returns></returns>
public static async Task<byte[]> GetBytesAsync(this IRandomAccessStream stream)
{
var bytes = new byte[stream.Size];
using (var reader = new DataReader(stream.GetInputStreamAt()))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(bytes);
return bytes;
}
} /// <summary>
/// Retrieves the pixel data.
/// </summary>
/// <param name="stream">The input stream.</param>
/// <returns></returns>
public static async Task<byte[]> GetPixelDataAsync(this IRandomAccessStream stream)
{
var decoder = await BitmapDecoder.CreateAsync(stream);
var provider = await decoder.GetPixelDataAsync();
return provider.DetachPixelData();
}
}
Byte array 转 IRandomAccessStream。
下面的两个方法用到了 WindowsRuntimeBufferExtensions 和 WindowsRuntimeStreamExtensions两个类的扩展方法。
需要引用System.Runtime.InteropServices.WindowsRuntime 和 System.IO 命名空间
public static IRandomAccessStream AsRandomAccessStream(this byte[] bytes)
{
return bytes.AsBuffer().AsStream().AsRandomAccessStream();
} public static IRandomAccessStream ConvertToRandomAccessStream(this byte[] bytes)
{
var stream = new MemoryStream(bytes);
return stream.AsRandomAccessStream();
}
How to resize or create a thumbnail image from file stream on UWP的更多相关文章
- Create Shortcut to Get Jar File Meta Information
You have to get meta information of cobertura.jar with command "unzip -q -c cobertura.jar META- ...
- CabArc to create or extract a cab file
CabArc n D:\test.cab D:\output\*.* CabArc x D:\test.cab -r -p D:\output\*.*
- create a large size empty file to measure transfer speed
OS : Windows open cmd fsutil file createnew file_name 1073741824 // 1GB fsutil file createnew file_n ...
- Stream 基础和常用
来源 : http://www.cnblogs.com/jimmyzheng/archive/2012/03/17/2402814.html 系列 目前只作为个人参考. 微软的 stream 结构老大 ...
- Windows下Thumbnail的开发总结
一.引言 Windows Thumbnail Handler是Windows平台下用来为关联的文件类型提供内容预览图的一套COM接口.通过实现Thumbnail相关的COM接口,就可以为为自定义的文件 ...
- How to Resize a Datafile (文档 ID 1029252.6)
APPLIES TO: Oracle Database - Enterprise Edition - Version 9.2.0.1 and laterInformation in this docu ...
- How To Create/Extend Swap Partition In Linux Using LVM
https://www.2daygeek.com/how-to-create-extend-swap-partition-in-linux-using-lvm/ BY RAMYA NUVVULA · ...
- create和grant配合使用,对Mysql进行创建用户和对用户授权
1.首先创建用户username以及密码passwd,授权主机localhost. create user ‘username’@'localhost' identified by 'passwd' ...
- Git – fatal: Unable to create ‘/.git/index.lock’: File exists错误解决办法
有时候在提交的时候,中间提交出错,导致有文件被lock,所以会报下面的错误: fatal: Unable to create ‘/msg/.git/index.lock’: File exists. ...
随机推荐
- Mybatis在oracle数据库中插入数据后返回自增值ID
1.将id设置成自增序列 CREATE OR REPLACE TRIGGER "DATALIB"."TRIG_USER_ADD" BEFORE INSERT O ...
- excel中vba将excel中数字和图表输出到word中
参考:https://wenku.baidu.com/view/6c60420ecc175527072208af.html 比如将选区变为图片保存到桌面: Sub 将选区转为图片存到桌面() Dim ...
- vue中的前置守卫
前置守卫是为了验证用户信息真实性,一些内容只能在用户登陆以后才能进行查看,例如个人中心,我的购物车,等个人页面,非隐私页面 用router.beforeEach进行验证,这个方法必须写在router实 ...
- 第07章:MongoDB-CRUD操作--文档--创建
①语法 insert() save() --有修改没有新增 insertOne() [3.2版本新增]向指定集合中插入一条文档数据 insertMany() [3.2版本新增]向指定集合中插入多条文 ...
- c#转换XML文件和json对象
创建.XML文件string xml = @"<?xml version=""1.0"" standalone=""no&q ...
- mybatis-generator扩展教程系列 -- 自定义generatorConfig.xml参数
http://blog.csdn.net/shadowsick/article/details/53413235
- noip第2课资料
- Tarjan缩点求入度为零的点的个数问题
Description: 一堆人需要联系,但如果x 可以联系 y,你联系了x就不用联系y了,你联系一个人都会有固定的花费,问你最小联系多少人,和最小花费 Solution: Tarjan缩点,求出缩点 ...
- html 语法
p: 源代码中包含多行,但是浏览器会忽略多行 <br />产生折行效果 h1:居中用样式实现 :水平线,有的很像border-top/bottom,或许用 实现更好 pre: 预格式化的文 ...
- node-lessons
教程:https://github.com/alsotang/node-lessons 0 nvm 的全称是 Node Version Manager,之所以需要这个工具,是因为 Node.js 的各 ...