How To Crop Bitmap For UWP
裁剪图片主要是借助于 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的更多相关文章
- How To Scan QRCode For UWP (4)
QR Code的全称是Quick Response Code,中文翻译为快速响应矩阵图码,有关它的简介可以查看维基百科. 我准备使用ZXing.Net来实现扫描二维码的功能,ZXing.Net在Cod ...
- Selenium&EmguCV实现爬虫图片识别
概述 爬虫需要抓取网站价格,与一般抓取网页区别的是抓取内容是通过AJAX加载,并且价格是通过CSS背景图片显示的. 每一个数字对应一个样式,如'p_h57_5' .p_h57_5 { backgrou ...
- WPF和Winform中picturebox图片局部放大
原文:WPF和Winform中picturebox图片局部放大 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/artic ...
- 2018-8-10-win10-uwp-如何创建修改保存位图
title author date CreateTime categories win10 uwp 如何创建修改保存位图 lindexi 2018-08-10 19:16:50 +0800 2018- ...
- [UWP]使用Writeable?Bitmap创建HSV色轮
原文:[UWP]使用Writeable?Bitmap创建HSV色轮 1. HSV 1.1 HSV的定义 HSV都是一种将RGB色彩模型中的点在圆柱坐标系中的表示法,这种表示法试图做到比RGB基于笛卡尔 ...
- UWP crop image control
最近做项目,需求做一个剪切图片的东东.如下图 主要是在一个canvas上面.根据crop的大小画出半透明的效果 <Canvas x:Name="imageCanvas" Vi ...
- [UWP]使用WriteableBitmap创建HSV色轮
1. HSV 1.1 HSV的定义 HSV都是一种将RGB色彩模型中的点在圆柱坐标系中的表示法,这种表示法试图做到比RGB基于笛卡尔坐标系的几何结构更加直观.HSV即色相.饱和度.明度(英语:Hue, ...
- UWP 浏览本地图片及对图片的裁剪
原文:UWP 浏览本地图片及对图片的裁剪 1.前言 准备给我的校园助手客户端添加一个修改头像的功能,但是查了好多资料都没有找到裁剪图片的简单的方法,最后才找到这个使用Launcher调用系统组件的简单 ...
- UWP 使用Windows.Web.Http命名空间下的HttpClient使用post方法,上传图片服务器
1.从相册里面选取图片 /// <summary> /// 1.1 从相册里面选取图片 /// </summary> /// <param name="send ...
随机推荐
- PHP时间范围:本周、本月、下月等简写
在阅读TP5.1源码,发现其在时间范围上的写法很特别,个人第一次见,做一次记录 $timeRule = [ 'today' => ['today', 'tomorrow'], 'yesterda ...
- IntelliJ IDEA 2017版 编译器使用学习笔记(七) (图文详尽版);IDE快捷键使用;IDE代码重构(编写高质量代码)
一.重构 重构变量:将语义模糊的变量名称改为更易理解的名称 修改变量名称,快键键 shift + F6 (输入要改的名字,所有位置相同的名字都会改变) 重构方法 ...
- 安卓中的makefile文件打印调试信息
在安卓源码的makefile中有很多变量的值不方便确定,那么可以通过调试makefile文件来确定这些变量的值. $(warning " TARGET_BOARD_PLATFORM = ...
- 安卓 build/core/Makefile 以及main.mk
android make 系统总共分为四层 arch board device product 在各个字android.mk文件中引用的定义都存放在./build/core/下!比如android.m ...
- redis的repl-ping-slave-period和repl-ping-replica-period
网上很多Redis方面的文章,会涉及到repl-ping-slave-period和repl-ping-replica-period这两个重要参数,从一些中文解释来看,意思差不多,即:SLAVE周期性 ...
- 在64位win10下安装32位oracle
乱试,居然搞定了. 1 下载个32位的驱动包,解压拷贝到C:\Windows\SysWOW64 其实这样就能用了:)不过如果人家只提供一个tsname的文件给你,或某些程序跑配置的,读取的配置是一个t ...
- C#-VS发布网站-准备待发布网站-摘
通过使用“发布网站”工具部署网站项目 准备网站源文件 在vs生成发布文件 配置IIS .NET Framework 4 其他版本 Visual Studio 2008 Visual Studio ...
- C++语言定义的标准转换
标准转换 C++ 语言定义其基础类型之间的转换. 它还定义指针.引用和指向成员的指针派生类型的转换. 这些转换称为“标准转换. 1. 整型提升 整数类型的对象可以转换为另一个更宽的整数类型(即,可表示 ...
- C++ 中的异常机制分析
C++异常机制概述 异常处理是C++的一项语言机制,用于在程序中处理异常事件.异常事件在C++中表示为异常对象.异常事件发生时,程序使用throw关键字抛出异常表达式,抛出点称为异常出现点,由操作系统 ...
- Ubuntu12搭建nutch1.2+tomcat7+jdk1.6
Ubuntu12搭建nutch1.2+tomcat7+jdk1.6 所用软件:jdk-6u24-linux-i586.bin apache-tomcat-7.0.27.tar.gz apache-nu ...