裁剪图片主要是借助于 BitmapDecoder.GetPixelDataAsync() 以及 BitmapTransform对象来实现。

实现的代码如下:

 using System;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.Foundation;
using Windows.Graphics.Imaging; namespace ScanQRCode
{
public static class ImageHelper
{
/// <summary>
/// Asynchronously get the cropped stream.
/// </summary>
/// <param name="inputStream">The input stream</param>
/// <param name="rect"></param>
/// <returns></returns>
public static async Task<IRandomAccessStream> GetCroppedStreamAsync(IRandomAccessStream inputStream, Rect rect)
{
if (inputStream == null)
return null; var startPointX = (uint)Math.Floor(rect.X);
var startPointY = (uint)Math.Floor(rect.Y);
var width = (uint)Math.Floor(rect.Width);
var height = (uint)Math.Floor(rect.Height); return await GetCroppedStreamAsync(inputStream, startPointX, startPointY, width, height);
} /// <summary>
/// Asynchronously get the cropped stream.
/// </summary>
/// <param name="inputStream">The input stream</param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static async Task<IRandomAccessStream> GetCroppedStreamAsync(IRandomAccessStream inputStream, uint x, uint y, uint width, uint height)
{
if (inputStream == null)
return null; var pixelData = await GetCroppedPixelDataAsync(inputStream, x, y, width, height);
if (pixelData == null)
return null; var outputStream = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outputStream);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, width, height, , , pixelData);
await encoder.FlushAsync(); return outputStream;
} /// <summary>
/// Asynchronously get the cropped pixel data.
/// </summary>
/// <param name="inputStream">The input stream</param>
/// <param name="rect"></param>
/// <returns></returns>
public static async Task<byte[]> GetCroppedPixelDataAsync(IRandomAccessStream inputStream, Rect rect)
{
if (inputStream == null)
return null; var startPointX = (uint)Math.Floor(rect.X);
var startPointY = (uint)Math.Floor(rect.Y);
var width = (uint)Math.Floor(rect.Width);
var height = (uint)Math.Floor(rect.Height); return await GetCroppedPixelDataAsync(inputStream, startPointX, startPointY, width, height);
} /// <summary>
/// Asynchronously get the cropped pixel data.
/// </summary>
/// <param name="inputStream">The input stream</param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static async Task<byte[]> GetCroppedPixelDataAsync(IRandomAccessStream inputStream, uint x, uint y, uint width, uint height)
{
if (inputStream == null)
return null; var decoder = await BitmapDecoder.CreateAsync(inputStream); // Refine the start point
if (x + width > decoder.PixelWidth)
x = decoder.PixelWidth - width;
if (y + height > decoder.PixelHeight)
y = decoder.PixelHeight - height; var transform = new BitmapTransform()
{
Bounds = new BitmapBounds
{
X = x,
Y = y,
Width = width,
Height = height
},
InterpolationMode = BitmapInterpolationMode.Fant,
ScaledWidth = decoder.PixelWidth,
ScaledHeight = decoder.PixelHeight
}; var pixelProvider = await decoder.GetPixelDataAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform,
ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); return pixelProvider.DetachPixelData();
}
}
}

How To Crop Bitmap For UWP的更多相关文章

  1. How To Scan QRCode For UWP (4)

    QR Code的全称是Quick Response Code,中文翻译为快速响应矩阵图码,有关它的简介可以查看维基百科. 我准备使用ZXing.Net来实现扫描二维码的功能,ZXing.Net在Cod ...

  2. Selenium&EmguCV实现爬虫图片识别

    概述 爬虫需要抓取网站价格,与一般抓取网页区别的是抓取内容是通过AJAX加载,并且价格是通过CSS背景图片显示的. 每一个数字对应一个样式,如'p_h57_5' .p_h57_5 { backgrou ...

  3. WPF和Winform中picturebox图片局部放大

    原文:WPF和Winform中picturebox图片局部放大 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/artic ...

  4. 2018-8-10-win10-uwp-如何创建修改保存位图

    title author date CreateTime categories win10 uwp 如何创建修改保存位图 lindexi 2018-08-10 19:16:50 +0800 2018- ...

  5. [UWP]使用Writeable?Bitmap创建HSV色轮

    原文:[UWP]使用Writeable?Bitmap创建HSV色轮 1. HSV 1.1 HSV的定义 HSV都是一种将RGB色彩模型中的点在圆柱坐标系中的表示法,这种表示法试图做到比RGB基于笛卡尔 ...

  6. UWP crop image control

    最近做项目,需求做一个剪切图片的东东.如下图 主要是在一个canvas上面.根据crop的大小画出半透明的效果 <Canvas x:Name="imageCanvas" Vi ...

  7. [UWP]使用Writeable​Bitmap创建HSV色轮

    1. HSV 1.1 HSV的定义 HSV都是一种将RGB色彩模型中的点在圆柱坐标系中的表示法,这种表示法试图做到比RGB基于笛卡尔坐标系的几何结构更加直观.HSV即色相.饱和度.明度(英语:Hue, ...

  8. UWP 浏览本地图片及对图片的裁剪

    原文:UWP 浏览本地图片及对图片的裁剪 1.前言 准备给我的校园助手客户端添加一个修改头像的功能,但是查了好多资料都没有找到裁剪图片的简单的方法,最后才找到这个使用Launcher调用系统组件的简单 ...

  9. UWP 使用Windows.Web.Http命名空间下的HttpClient使用post方法,上传图片服务器

    1.从相册里面选取图片 /// <summary> /// 1.1 从相册里面选取图片 /// </summary> /// <param name="send ...

随机推荐

  1. mysql (_mysql_exceptions.OperationalError) (1055, "Expression #1 of SELECT list is not in GROUP BY clause

    sudo gedit /etc/mysql/my.cnf在打开的my.cnf文件中添加 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 保存,退 ...

  2. C语言编程题

    1.将整形a的第m位赋值到整形b的第n位 int fun4(int a, int m, int b, int n) { a = (a>>m) & ;//将a的第m位取出,为1或0, ...

  3. BitMap的简单实现

    面试结束的这些日子好几次接触到BitMap这个东西.到底是啥呢,究其原因就是虽然它的使用条件较为苛刻,但是它对应的时间复杂度和空间复杂度真的是惊人的好. 首先是根据其思想先写了一个比较差的实现代码: ...

  4. javascript 根据 两点 经纬度 测出距离

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. 实战--利用SVM对基因表达标本是否癌变的预测

    利用支持向量机对基因表达标本是否癌变的预测 As we mentioned earlier, gene expression analysis has a wide variety of applic ...

  6. Shell编程-10-Shell中的数组

    目录 数组基础 数组示例 数组总结     如果大家有其他语言的基础或经验,就很快能明白数组了.简单来说,数组就某一种相同类型的元素组合,而后通过下标对其进行访问各元素. 数组基础 基础语法 第一种形 ...

  7. Protocol Buffer 序列化原理大揭秘 - 为什么Protocol Buffer性能这么好?

    前言 习惯用 Json.XML 数据存储格式的你们,相信大多都没听过Protocol Buffer Protocol Buffer 其实 是 Google出品的一种轻量 & 高效的结构化数据存 ...

  8. Hdu1083 Courses

    Courses Problem Description Consider a group of N students and P courses. Each student visits zero, ...

  9. Naive Bayes 笔记

    Naive Bayes (朴素贝叶斯) 属于监督学习算法, 它通过计算测试样本在训练样本各个分类中的概率来确定测试样本所属分类, 取最大概率为其所属分类.  优点  在数据较少的情况下仍然有效,可以处 ...

  10. 解决.net+steeltoe服务客户端被服务调用出现400BadRequst错误

    一直尝试用steeltoe的官方示例被调用,一直报400BadRequst错误,换用Java写了一个简单client服务,却能正常被调用. 百思不得其解,用了一晚上填坑,开始觉得是不是IP没绑定,服务 ...