如何在Rust中保存PNG图像? [英] How to save a PNG image in Rust?

查看:238
本文介绍了如何在Rust中保存PNG图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 u8 字节的向量(每像素4个字节 - RGBA),如何将其保存到PNG文件中?

Given a vector of u8 bytes (4 bytes per pixel - RGBA), how can this be saved to a PNG file?

推荐答案

您可以使用Piston存储库中的图像包来保存原始缓冲区到磁盘。

You could use the image crate in the Piston repository to save the raw buffer to disk.

页面底部的示例显示了如何执行此操作..:

The example at the bottom of the page shows you how to do this..:

extern crate image;

fn main() {

    let buffer: &[u8] = ...; // Generate the image data

    // Save the buffer as "image.png"
    image::save_buffer(&Path::new("image.png"), buffer, 800, 600, image::RGBA(8))
}

这篇关于如何在Rust中保存PNG图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆