2018-8-10-win10-uwp-读取保存WriteableBitmap-、BitmapImage
title | author | date | CreateTime | categories |
---|---|---|---|---|
win10 uwp 读取保存WriteableBitmap 、BitmapImage
|
lindexi
|
2018-08-10 19:16:51 +0800
|
2018-2-13 17:23:3 +0800
|
Win10 UWP
|
我们在UWP,经常使用的图片,数据结构就是 BitmapImage 和 WriteableBitmap。关于 BitmapImage 和 WriteableBitmap 区别,我就不在这里说。主要说的是 BitmapImage 和 WriteableBitmap 、二进制 byte 的互转。
我们先写一个简单的xaml
<Image x:Name="Img" Height="200" Width="200"
HorizontalAlignment="Center" Source="Assets/SplashScreen.png" ></Image> <Button Margin="10,300,10,10" Content="确定" Click="Button_OnClick" ></Button>
用到的图片是我新建自带的。
保存 WriteableBitmap 到文件
private static async Task SaveWriteableBitmapImageFile(WriteableBitmap image, StorageFile file)
{
//BitmapEncoder 存放格式
Guid bitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
string filename = file.Name;
if (filename.EndsWith("jpg"))
{
bitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
}
else if (filename.EndsWith("png"))
{
bitmapEncoderGuid = BitmapEncoder.PngEncoderId;
}
else if (filename.EndsWith("bmp"))
{
bitmapEncoderGuid = BitmapEncoder.BmpEncoderId;
}
else if (filename.EndsWith("tiff"))
{
bitmapEncoderGuid = BitmapEncoder.TiffEncoderId;
}
else if (filename.EndsWith("gif"))
{
bitmapEncoderGuid = BitmapEncoder.GifEncoderId;
}
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite, StorageOpenOptions.None))
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(bitmapEncoderGuid, stream);
Stream pixelStream = image.PixelBuffer.AsStream();
byte[] pixels = new byte[pixelStream.Length];
await pixelStream.ReadAsync(pixels, 0, pixels.Length);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
(uint)image.PixelWidth,
(uint)image.PixelHeight,
96.0,
96.0,
pixels);
//Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(imgstream);
//Windows.Graphics.Imaging.PixelDataProvider pxprd = await decoder.GetPixelDataAsync(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Straight, new Windows.Graphics.Imaging.BitmapTransform(), Windows.Graphics.Imaging.ExifOrientationMode.RespectExifOrientation, Windows.Graphics.Imaging.ColorManagementMode.DoNotColorManage);
await encoder.FlushAsync();
}
}
从文件读 WriteableBitmap
private static async Task<WriteableBitmap> OpenWriteableBitmapFile(StorageFile file)
{
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
WriteableBitmap image = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
image.SetSource(stream); return image;
}
}
ImageSource 转byte[]
ImageSource可以是 BitmapImage 、WriteableBitmap,如果是WriteableBitmap ,那么直接转换
WriteableBitmap 转byte[]
bitmap.PixelBuffer.ToArray();
Image 转byte[]
如果我们的 ImageSource 是 BitmapImage ,那么我们不能使用上面的办法,直接保存 WriteableBitmap ,我们可以使用截图
private async Task<string> ToBase64(Image control)
{
var bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(control);
return await ToBase64(bitmap);
}
如果 ImageSource 是 WriteableBitmap ,直接保存
我们使用 byte[] 在传输时不好,不能用在 http 传输上(不是一定的不能),所以我们就把它转为base64,我提供了很多方法把数组转 base64 ,把文件转为 base64 。代码是 https://codepaste.net/ijx28i 抄的。
//WriteableBitmap 转 byte[]
private async Task<string> ToBase64(WriteableBitmap bitmap)
{
var bytes = bitmap.PixelBuffer.ToArray();
return await ToBase64(bytes, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight);
} private async Task<string> ToBase64(StorageFile bitmap)
{
var stream = await bitmap.OpenAsync(Windows.Storage.FileAccessMode.Read);
var decoder = await BitmapDecoder.CreateAsync(stream);
var pixels = await decoder.GetPixelDataAsync();
var bytes = pixels.DetachPixelData();
return await ToBase64(bytes, (uint)decoder.PixelWidth, (uint)decoder.PixelHeight, decoder.DpiX, decoder.DpiY);
} private async Task<string> ToBase64(RenderTargetBitmap bitmap)
{
var bytes = (await bitmap.GetPixelsAsync()).ToArray();
return await ToBase64(bytes, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight);
} private async Task<string> ToBase64(byte[] image, uint height, uint width, double dpiX = 96, double dpiY = 96)
{
// encode image
var encoded = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, encoded);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, height, width, dpiX, dpiY, image);
await encoder.FlushAsync();
encoded.Seek(0); // read bytes
var bytes = new byte[encoded.Size];
await encoded.AsStream().ReadAsync(bytes, 0, bytes.Length); // create base64
return Convert.ToBase64String(bytes);
} private async Task<ImageSource> FromBase64(string base64)
{
// read stream
var bytes = Convert.FromBase64String(base64);
var image = bytes.AsBuffer().AsStream().AsRandomAccessStream(); // decode image
var decoder = await BitmapDecoder.CreateAsync(image);
image.Seek(0); // create bitmap
var output = new WriteableBitmap((int)decoder.PixelHeight, (int)decoder.PixelWidth);
await output.SetSourceAsync(image);
return output;
}
上面代码出处:https://codepaste.net/ijx28i
从文件读 BitmapImage
private async Task<BitmapImage> OpenBitmapImageFile(StorageFile file)
{
var fileStream = await file.OpenReadAsync();
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(fileStream);
return bitmap;
}
BitmapImage 转 WriteableBitmap
我使用http://www.cnblogs.com/cjw1115/p/5164327.html 大神的,直接转WriteableBitmap bitmap = imageSource as WriteableBitmap;
bitmap为null,于是我在网上继续找,好像没看到 UWP 的可以转,只有win7的
其实大神有说,Image的 Source是 WriteableBitmap ,于是他就能转。
UWP的 BitmapImage 不能转换为 byte[] 或 WriteableBitmap 。这句话是错的。
2017年1月4日21:45:37
我后来过了几个月,发现我们的 BitmapImage 可以转 byte[]
我们可以通过拿 BitmapImage 的 UriSource 把它转为 WriteableBitmap ,可以使用截图获得 BitmapImage。
如果想要使用 BitmapImage 的 UriSource 转为 WriteableBitmap,需要 WriteableBitmapEx 。他是在 WPF 就被大家喜欢的库。如何安装 WriteableBitmapEx ,其实有了Nuget 基本没问题。
搜索 WriteableBitmapEx Nuget
然后搜索到了,我们要什么,好像我也不知道。
我就知道可以使用 WriteableBitmap image = await BitmapFactory.New(1, 1).FromContent((BitmapImage).UriSource);
那么转 byte[] 如何做,有了 WriteableBitmap ,下面的我也不知道,不要问我。
如果使用 BitmapImage 图片是 SetSource,那么我也不会。
获取图片中鼠标点击的颜色
获取鼠标点击的那个点,图片的颜色。那么图片之外,界面呢?其实我们还可以把界面截图,然后获取。
那么我们需要首先在 Image 使用 Tap ,假如图片 source 是 BitmapImage
前提安装 WriteableBitmapEx ,假如我们的 ViewModel有一个 BitmapImage 的图片 Image ,于是我们可以使用
var position = e.GetPosition(sender as UIElement); //鼠标点击的在哪 WriteableBitmap image = await BitmapFactory.New(1, 1).FromContent((View.Image).UriSource); //我上面说的如何把 BitmapImage 转 WriteableBitmapEx var temp = image.GetPixel((int) position.X, (int) position.Y); string str = $"R: {temp.R} G: {temp.G} B: {temp.B} ";
获得图片中鼠标点击的颜色。这个方法有时炸了,都是 255 。
代码:https://github.com/lindexi/UWP/tree/master/uwp/src/ImageMoseClick
获取Dpi
可以使用下面代码获取图片DPI。
我的图片从解决方案获得,大家可以从任意的位置获取,只要可以转换为 IRandomAccessStream
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/lindexi.png"));
using (IRandomAccessStream stream = await file.OpenReadAsync())
{
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.PngDecoderId, stream);
var DpiX = decoder.DpiX;
var DpiY = decoder.DpiY;
}
如果需要保存网络图片到本地,请到win10 uwp 存放网络图片到本地
参见:http://www.cnblogs.com/cjw1115/p/5164327.html
2018-8-10-win10-uwp-读取保存WriteableBitmap-、BitmapImage的更多相关文章
- win10 uwp 读取保存WriteableBitmap 、BitmapImage
我们在UWP,经常使用的图片,数据结构就是 BitmapImage 和 WriteableBitmap.关于 BitmapImage 和 WriteableBitmap 区别,我就不在这里说.主要说的 ...
- win10 uwp 读取文本GBK错误
本文讲的是解决UWP文本GBK打开乱码错误,如何去读取GBK,包括网页GBK.最后本文给出一个方法追加文本. 我使用NotePad记事本保存文件,格式ASCII,用微软示例打开文件方式读取,出现错误 ...
- win10 uwp 读取resw资源文件
ResourceContext resourceContext = ResourceContext.GetForViewIndependentUse(); ResourceMap resourceMa ...
- win10 uwp 入门
UWP是什么我在这里就不说,本文主要是介绍如何入门UWP,也是合并我写的博客. 关于UWP介绍可以参见:http://lib.csdn.net/article/csharp/32451 首先需要申请一 ...
- win10 uwp 手把手教你使用 asp dotnet core 做 cs 程序
本文是一个非常简单的博客,让大家知道如何使用 asp dot net core 做后台,使用 UWP 或 WPF 等做前台. 本文因为没有什么业务,也不想做管理系统,所以看到起来是很简单. Visua ...
- win10 uwp 使用 Microsoft.Graph 发送邮件
在 2018 年 10 月 13 号参加了 张队长 的 Office 365 训练营 学习如何开发 Office 365 插件和 OAuth 2.0 开发,于是我就使用 UWP 尝试使用 Micros ...
- win10 UWP 序列化
将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象. .NET Framewor ...
- 【广告】win10 uwp 水印图床 含代码
本文主要是广告我的软件. 图床可以加速大家写博客上传图片的时间,通过简化我们的操作来得到加速. 在写博客的时候,我们发现,我们需要上传一张图片,需要先打开图片,然后选择本地图片,然后上传. 但是我经常 ...
- win10 uwp 商业游戏
本文告诉大家去做一个商业游戏,游戏很简单,几乎没有什么技术 游戏的开始,需要添加框架库,于是引用我自己写的库. 首先是创建一个启动页面,这个页面是显示启动的. 在显示启动的时候,是需要加载游戏需要使用 ...
- 01 mybatis框架整体概况(2018.7.10)-
01 mybatis框架整体概况(2018.7.10)- F:\廖雪峰 JavaEE 企业级分布式高级架构师课程\廖雪峰JavaEE一期\第一课(2018.7.10) maven用的是3.39的版本 ...
随机推荐
- LINUX下C++编程如何获得某进程的ID
#include <stdio.h> #include <stdlib.h> #include <unistd.h> using namespace std; pi ...
- Windows Phpstrom svn 配置
网上百度找到的解决方案行不通,就是下图两项都不选中.临时是可以的,但是到了第二天,又不行了. 以下是自己瞎弄的,居然可以了. 第一步:安装TortoiseSVN 1.8.* ,注意安装选项要选上com ...
- webpack学习之—— 依赖图(Dependency Graph) 及 构建目标(Targets)
Dependency Graph 任何时候,一个文件依赖于另一个文件,webpack 就把此视为文件之间有依赖关系.这使得 webpack 可以接收非代码资源(non-code asset)(例如图像 ...
- JavaScript之HTML DOM Document 对象
文档对象代表您的网页. 如果您希望访问 HTML 页面中的任何元素,那么您总是从访问 document 对象开始. 下面是一些如何使用 document 对象来访问和操作 HTML 的实例. 查找 H ...
- IntelliJ IDEA 下的SVN使用(傻瓜式教学)(转)
第一步:下载svn的客户端,通俗一点来说就是小乌龟啦!去电脑管理的软件管理里面可以直接下载,方便迅速 下载之后直接安装就好了,但是要注意这里的这个文件也要安装上,默认是不安装的,如果不安装,svn中的 ...
- Loadrunner常用分析点
Loadrunner常用的分析点 一.在Vuser(虚拟用户状态)中 1.Running Vusers:提供了生产负载的虚拟用户运行状态的相关信息,可以帮助我们了解负载生成的结果.(即用户在几分钟左右 ...
- iOS 使用Quartz和OpenGL绘图
http://blog.csdn.net/coder9999/article/details/7641701 第十二章 使用Quartz和OpenGL绘图 有时应用程序需要能够自定义绘图.一个库是Qu ...
- 云计算、大数据、编程语言学习指南下载,100+技术课程免费学!这份诚意满满的新年技术大礼包,你Get了吗?
开发者认证.云学院.技术社群,更多精彩,尽在开发者会场 近年来,新技术发展迅速.互联网行业持续高速增长,平均薪资水平持续提升,互联网技术学习已俨然成为学生.在职人员都感兴趣的“业余项目”. 阿里云大学 ...
- 外贸电子商务网站之Prestashop 语言包安装
prestashop添加语言-下载语言包 我们找到中文简体(Chinese-Simplified)一行,点击最后一栏的下载(Download)按钮,我们点击下载,可以下到一个以语言的 ISO为文件名, ...
- python批量导出项目依赖包及批量安装的方法
在Python中我们在项目中会用到各种库,自带的自然不必再说,然而如果是三方库,则在进行项目移植时通常需要在新的环境下安装需要的三方库文件,面对较大项目中众多的三方库,可以先将项目依赖库导出到txt文 ...