裁剪图片主要是借助于 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. Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)

    Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...

  2. c# 产生随机数 程序所在路径

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. 安卓修改开机logo和开机动画的方法

    第一种和第二种方法亲测可用,安卓版本是4.2和安卓5.1均可.第二种方法待验证 以下三种方法 Android 开机其实总共会出现3个画面: 1.第一个就是 linux 系统启动,出现Linux小企鹅画 ...

  4. ubuntu下非交互式安装MySQL

    $ sudo su -# MYSQL_PASS=nova 设定mysql的密码和nova数据库的密码# cat <<MYSQL_PRESEED | debconf-set-selectio ...

  5. winSockets编程(七)WSAAsyncSelect模式

    占位## #include <WinSock2.h> #include <Windows.h> #include <StrSafe.h> #pragma comme ...

  6. winSocket编程(一)WSAStartup

    /******************************************************************** 更新日期:2017-11-07 10:33:08* 进度:完 ...

  7. 为在python中使用dotnet程序安装clr

    直接在命令行中录入 pip install pythonnet 会有一个ssl错误提示 ...Could not fetch URL https://pypi.python.org/simple/py ...

  8. c简单的单向链表

    ps:list链表  node节点 在链表中节点就是一个个的结构体 堆空间由于在申请内存时,地址是随机的,所以要用链表的方式将其连接起来,但是链表头的地址要知道. 每个节点包含两个部分:数据区和地址区 ...

  9. linux 环境安装及学习

    一.Httpd Linux(Apache)Httpd服务器安装,启动及httpd.conf配置详解   https://blog.51cto.com/itwish/2160492 httpd-2.4. ...

  10. Brain Rules: 12 Principles for Surviving and Thriving at Work, Home, and School

    选自 https://litemind.com/brain-rules/