如何将透明填充添加到 jpg 并将其保存为透明的 png?
How to add transparent padding to a jpg and save it as png with transparency?
SixLabors ImageSharp 的文档非常有限,大多数 google 搜索导致 GitHub,这不是很有帮助。
如何上传 jpg,.Mutate
它带有透明填充并将其另存为透明的 png?
这是我目前的代码。如果上传的图像是 png,透明填充有效,但 jpgs 得到黑色填充:
private static void ResizeAndSavePhoto(Image<Rgba32> img, string path, int squareSize)
{
Configuration.Default.ImageFormatsManager.SetEncoder(PngFormat.Instance, new PngEncoder()
{
ColorType = PngColorType.RgbWithAlpha
});
img.Mutate(x =>
x.Resize(new ResizeOptions
{
Size = new Size(squareSize, squareSize),
Mode = ResizeMode.Pad
}).BackgroundColor(new Rgba32(255, 255, 255, 0))
);
img.Save(path);
return;
}
.SaveAsPng()
采用文件流,但我有一个 Image<Rgba32>
和一个路径...
您可以通过 SaveAsPng
显式另存为 png,将路径扩展设置为 .png
,或将 IImageEncoder
传递给 Save
方法。
您将在 https://docs.sixlabors.com/api/index.html
找到 API 文档
private static void ResizeAndSavePhoto(Image<Rgba32> img, string path, int squareSize)
{
img.Mutate(x =>
x.Resize(new ResizeOptions
{
Size = new Size(squareSize, squareSize),
Mode = ResizeMode.Pad
}).BackgroundColor(new Rgba32(255, 255, 255, 0)));
// The following demonstrates how to force png encoding with a path.
img.Save(Path.ChangeExtension(path, ".jpg"))
img.Save(path, new PngEncoder());
}
此外,如果保存到流。
img.SaveAsPng(path);
SixLabors ImageSharp 的文档非常有限,大多数 google 搜索导致 GitHub,这不是很有帮助。
如何上传 jpg,.Mutate
它带有透明填充并将其另存为透明的 png?
这是我目前的代码。如果上传的图像是 png,透明填充有效,但 jpgs 得到黑色填充:
private static void ResizeAndSavePhoto(Image<Rgba32> img, string path, int squareSize)
{
Configuration.Default.ImageFormatsManager.SetEncoder(PngFormat.Instance, new PngEncoder()
{
ColorType = PngColorType.RgbWithAlpha
});
img.Mutate(x =>
x.Resize(new ResizeOptions
{
Size = new Size(squareSize, squareSize),
Mode = ResizeMode.Pad
}).BackgroundColor(new Rgba32(255, 255, 255, 0))
);
img.Save(path);
return;
}
.SaveAsPng()
采用文件流,但我有一个 Image<Rgba32>
和一个路径...
您可以通过 SaveAsPng
显式另存为 png,将路径扩展设置为 .png
,或将 IImageEncoder
传递给 Save
方法。
您将在 https://docs.sixlabors.com/api/index.html
找到 API 文档private static void ResizeAndSavePhoto(Image<Rgba32> img, string path, int squareSize)
{
img.Mutate(x =>
x.Resize(new ResizeOptions
{
Size = new Size(squareSize, squareSize),
Mode = ResizeMode.Pad
}).BackgroundColor(new Rgba32(255, 255, 255, 0)));
// The following demonstrates how to force png encoding with a path.
img.Save(Path.ChangeExtension(path, ".jpg"))
img.Save(path, new PngEncoder());
}
此外,如果保存到流。
img.SaveAsPng(path);