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 ...
随机推荐
- openstack 租户、用户管理
创建domain [root@cc01 ~]# openstack domain create --description "Default Domain" default +-- ...
- c# 动态数组-----“动态”数组
其实在大多数工作中我们能通过前处理来确定我们的数组有多大,这样我们就可以声明相应大小的数组了.我感觉这种“动态”数组就够我用了.比如我要处理excel中数据,数据有m行*n列,这样我就可以通过读取ex ...
- IntelliJ IDEA 2017版 spring-boot 2.03 去除控制台logo;去除springboot 图标;去除springboot 图
1.控制台出现logo图标 2.如果不喜欢,如何去除这个logo 在其Application中进行设置 package com.springboot.logo; import org.springfr ...
- what is a resolver
resolver [rɪ'zɒlvə] 解析器 ViewResolver The ViewResolver provides a mapping between view names and actu ...
- Linux后台开发工具箱
https://files-cdn.cnblogs.com/files/aquester/Linux后台开发工具箱.pdf 目录 目录 1 1. 前言 3 2. 脚本类工具 3 2.1. sed命令- ...
- classmethod,staticmethod
'''1 绑定方法: 在类内部定义的函数,默认就是给对象来用,而且是绑定给对象用的,称为对象的绑定方法 绑定对象的方法特殊之处: 应该由对象来调用,对象来调用,会自动将对象当作第一个参数传入 绑定到类 ...
- Miller_raibin算法随机化检测素数 & Pollar_rho 算法分解大数
这几天一直再学习这些内容,也没有发一些博客,现在我觉得差不多了 首先基础是Miller_raibin随机化检测素数,顾名思义,随机化也就是有几率不对,但是很低,适用于大数快速检测,因为大数已经超出了我 ...
- TVS(瞬间电压抑制器)
1.原理 TVS二极管在线路板上与被保护线路并联,当瞬时电压超过电路正常工作电压后,TVS二极管便产生雪崩,提供给瞬时电流一个超低电阻通路,其结果是瞬时电流透过二极管被引开,避开被保护元件,并且在电压 ...
- cxgrid过滤使用心得
uses cxFilter; cxgrid过滤条件清除:cxgrdbtblvwGrid1DBTableView2.DataController.Filter.AutoDataSetFilter:=Tr ...
- 让你的照片更鲜艳------hsv拉伸
如果你的照片看上去灰蒙蒙的,缺少生机,那么hsv拉伸也许可以帮你的忙.hsv拉伸是一种可以提高图像鲜艳程度的图像增强方法,它能够让图像的颜色更加鲜活.艳丽,而且它的处理结果看上去很自然,比如源图中较暗 ...