• 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 文件

  • 相关阅读:
    ubuntu(linux)虚拟主机部署桌面,使用window链接
    扫描shader
    Android Studio快捷键
    eclipse取消空格、等号、分号自动录入
    Libgdx学习记录28——创建Desktop程序
    设计模式19——代理模式
    设计模式18——模板方法
    设计模式17——解释器模式
    设计模式16——工厂模式
    设计模式15——外观模式
  • 原文地址:https://www.cnblogs.com/lindexi/p/12085860.html
Copyright © 2020-2023  润新知