我们在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

http://www.cnblogs.com/yuanforprogram/p/4819307.html

http://stackoverflow.com/questions/41439543/how-can-i-get-the-pixel-color-of-an-image-at-the-current-pointer-position-in-a-u

http://lindexi.oschina.io/lindexi/post/win10-uwp-%E8%AF%BB%E5%8F%96%E4%BF%9D%E5%AD%98WriteableBitmap-BitmapImage/

http://www.cnblogs.com/mqxs/p/5707620.html


本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

win10 uwp 读取保存WriteableBitmap 、BitmapImage的更多相关文章

  1. win10 uwp 读取文本GBK错误

    本文讲的是解决UWP文本GBK打开乱码错误,如何去读取GBK,包括网页GBK.最后本文给出一个方法追加文本. 我使用NotePad记事本保存文件,格式ASCII,用微软示例打开文件方式读取,出现错误 ...

  2. win10 uwp 读取resw资源文件

    ResourceContext resourceContext = ResourceContext.GetForViewIndependentUse(); ResourceMap resourceMa ...

  3. win10 uwp 入门

    UWP是什么我在这里就不说,本文主要是介绍如何入门UWP,也是合并我写的博客. 关于UWP介绍可以参见:http://lib.csdn.net/article/csharp/32451 首先需要申请一 ...

  4. win10 UWP 剪贴板 Clipboard

    win10 UWP 剪贴板 Clipboard使用Windows.ApplicationModel.DataTransfer.Clipboard 设置文本 DataPackage dataPackag ...

  5. win10 UWP 序列化

    将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象. .NET Framewor ...

  6. win10 uwp 手把手教你使用 asp dotnet core 做 cs 程序

    本文是一个非常简单的博客,让大家知道如何使用 asp dot net core 做后台,使用 UWP 或 WPF 等做前台. 本文因为没有什么业务,也不想做管理系统,所以看到起来是很简单. Visua ...

  7. Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App

    安装VS2015 Update2的过程是非常曲折的.还好经过不懈的努力,终于折腾成功了. 如果开发Cordova项目的话,推荐大家用一下ionic这个框架,效果还不错.对于Cordova.PhoneG ...

  8. 【Win10 UWP】QQ SDK(二):SDK的回调处理

    上一讲,我们介绍了QQ SDK的使用方法,请看<[Win10 UWP]QQ SDK(一):SDK基本使用方法> 一. 回调的基本形式 从前面的介绍中我们知道,我们的应用和QQ客户端之间需要 ...

  9. win10 uwp 读写XML

    UWP 对 读写 XML做了一些修改,但和之前 WPF 的方法没有大的区别. 我们先来说下什么是 XML , XML 其实是 树结构,可以表达复杂的结构,所以在定制要求高的.或其他方面如json 做不 ...

随机推荐

  1. 201521123036 《Java程序设计》第7周学习总结

    本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 answer:用于判断Collect ...

  2. 201521123029《Java程序设计》第七周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料:XMind 答: 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contain ...

  3. 201521123003《Java程序设计》第4周学习总结

    1. 本章学习总结 你对于本章知识的学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 参考资料: 百度脑图 XMind 1.2 使用常规方法总结其他上课内容. (1)了解了类型转换(cast) ...

  4. 201521123010 《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 ①互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1 ...

  5. JAVA课程设计——团队博客

    JAVA课程设计--团队博客 1. 团队名称.团队成员介绍(需要有照片) 团队名称:"小羊吃蓝莓"小游戏 团队成员介绍: 成员 班级 学号 廖怡洁 网络1513 201521123 ...

  6. 201521123060 《Java程序设计》第14周学习总结

    1.本周学习总结 1.1以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2.书面作业 1.MySQL数据库基本操作 1.1建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己 ...

  7. java课程设计---彩票销售管理系统

    彩票购买销售系统 1.项目git地址 https://git.oschina.net/fenm/lotterry.git 部分项目地址提交截图 项目主要功能图 团队博客链接 http://www.cn ...

  8. 多线程面试题系列(3):原子操作 Interlocked系列函数

    上一篇中讲到一个多线程报数功能.为了描述方便和代码简洁起见,我们可以只输出最后的报数结果来观察程序是否运行出错.这也非常类似于统计一个网站每天有多少用户登录,每个用户登录用一个线程模拟,线程运行时会将 ...

  9. Hibernate第六篇【多对多映射、一对一映射】

    前言 前面已经讲解了一对多和多对一的映射是怎么配置了,也讲解了inverse和cascade属性对关联关系的影响,本博文讲解多对多的映射和一对一的映射! 多对多映射 需求:一个项目由多个员工开发,一个 ...

  10. Python-老男孩-03_socket

    Socket简介: 所谓Socket也称"套接字",用于描述IP和端口,是一个通信链的句柄,应用程序通过"套接字"向网络发出请求或应答网络请求. Socket起 ...