简体   繁体   English

是否可以使用 SixLabors.ImageSharp 库转换图像格式?

[英]Is it possible to convert image formats using the SixLabors.ImageSharp Libraries?

I have been studying this library with the intent to make a image processing pipeline in C#.我一直在研究这个库,目的是在 C# 中创建一个图像处理管道。 I would want it to be able to receive a .png image and convert it to a .jpeg.我希望它能够接收 .png 图像并将其转换为 .jpeg。 Yet I have not found a way to do this using this library.但是我还没有找到使用这个库来做到这一点的方法。 Is it even possible?甚至有可能吗? Has anyone been able to do this successfully?有没有人能够成功地做到这一点?

This was my best shot:这是我最好的镜头:

Image<Rgba32> image1 = SixLabors.ImageSharp.Image.Load<Rgba32>(fileName, out IImageFormat format);

var encd = new JpegEncoder
                {
                    Quality = 80
                };

image.Save(outputFinalPath, encd);

The result is the image simply being saved as a .png.结果是图像被简单地保存为 .png。 I'm new to working with images so I might be missing something.我是处理图像的新手,所以我可能会遗漏一些东西。

all you have to do to load an image from any PNG file then call save passing a path to a file with a jpg file extension.您只需从任何 PNG 文件加载图像,然后调用 save 将路径传递到具有 jpg 文件扩展名的文件。

using (var img = Image.Load("path/to/inputImage.png"))
{
    img.Save("path/to/outputImage.jpeg");
}

optionally passing a jpeg encoder to tweak settings, or you could also just change the defaults using the below code可选地传递 jpeg 编码器来调整设置,或者您也可以使用以下代码更改默认值

Configuration.Default.ImageFormatsManager.SetEncoder(JpegFormat.Instance, new JpegEncoder()
{
    Quality = 90
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM