zoukankan      html  css  js  c++  java
  • 2019-3-25-win10-uwp-如何将像素数组转-png-文件

    title author date CreateTime categories
    win10 uwp 如何将像素数组转 png 文件
    lindexi
    2019-3-25 8:53:1 +0800
    2019-3-25 8:53:1 +0800
    Win10 UWP

    堆栈的小伙伴好奇他有一个数组,数组里面是 BGRA 的像素,他需要将这个数组转换为 PNG 文件 在 UWP 可以使用 BitmapEncoder 将像素数组加密为文件

    在使用 BitmapEncoder 之前需要要求有像素数组,像素数组的规律有要求,按照 BGRA 按照顺序的数组,同时要求知道像素的原图的像素宽度。因为存放像素数组使用的是一维的数组,如果不知道图片宽度,那么就不知道这个图片的像素是对应数组哪个

    通过下面方法可以转换像素数组到文件

            private async Task ByteToPng(byte[] byteList, int width, int height, IRandomAccessStream file)
            {
                try
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, file);
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint) width, (uint) height, 96,
                        96, byteList);
                    await encoder.FlushAsync();
                }
                catch (Exception e)
                {
                }
            }

    这里的 IRandomAccessStream 就是 StorageFile 打开文件

            private async Task SaveToFileAsync(byte[] byteList, int width, int height, IStorageFile file)
            {
                using (var stream = (await file.OpenStreamForWriteAsync()).AsRandomAccessStream())
                {
                    await ByteToPng(byteList, width, height, stream);
                }
            }

    通过这个方法,可以传入数组和图片的宽度和高度,保存的文件,就可以将像素数组保存到 png 文件

  • 相关阅读:
    NGINX高并发配置
    一致性哈希算法——PHP实现代码
    TCP三次握手详解
    一致性哈希算法——转载来自张洋
    ngx_http_upstream_keepalive
    高情商人的十一种表现
    Nginx中的upstream轮询机制介绍
    主从读写分离----mysql-proxy0.8.5安装与配置
    分布式事务XA
    微信小程序API
  • 原文地址:https://www.cnblogs.com/lindexi/p/12085860.html
Copyright © 2011-2022 走看看