C# 使用Magick.NET拉伸图像以填充透明背景
我有一张照片(附件)。 我希望图像被拉伸到覆盖黄色区域 我用的是c#C# 使用Magick.NET拉伸图像以填充透明背景,c#,image,imagemagick,magick.net,C#,Image,Imagemagick,Magick.net,我有一张照片(附件)。 我希望图像被拉伸到覆盖黄色区域 我用的是c# 最好的方法是什么?我建议以下方法: 读取输入图像保存原始大小 使用trim 将(现在更小的)图像拉伸到原始大小 *如果示例图像中的黄色区域实际上是透明的,则可以在以下代码中保留fuzz=0,否则必须调整该值以确保删除所有不需要的区域 string srcImageFullPath = "c:\input.png"; int fuzz = 0; string destImageFullPath = "c:\output.png
最好的方法是什么?我建议以下方法:
trim
fuzz=0
,否则必须调整该值以确保删除所有不需要的区域
string srcImageFullPath = "c:\input.png";
int fuzz = 0;
string destImageFullPath = "c:\output.png";
// Read image from file
using (MagickImage image = new MagickImage(srcImageFullPath))
{
//save height/width of the original image
int height = image.Page.Height;
int width = image.Page.Width;
//set fuzz percentage
image.ColorFuzz = new ImageMagick.Percentage(fuzz);
//trim borders
image.Trim();
//resize image to original size
MagickGeometry size = new MagickGeometry(width, height);
size.IgnoreAspectRatio = true;
image.Resize(size);
// Save the result
image.Write(destImageFullPath);
}
在下图中,您可以在左侧看到原始图像,在右侧看到调整大小后的图像:
注释
Trim
删除与图像角落像素颜色相同的任何边框(有关详细信息,请参阅)Fuzz
删除“相似”颜色(详细信息)。如前所述,如果您的边框是透明的,只需离开fuzz=0
我建议采取以下办法:
trim
fuzz=0
,否则必须调整该值以确保删除所有不需要的区域
string srcImageFullPath = "c:\input.png";
int fuzz = 0;
string destImageFullPath = "c:\output.png";
// Read image from file
using (MagickImage image = new MagickImage(srcImageFullPath))
{
//save height/width of the original image
int height = image.Page.Height;
int width = image.Page.Width;
//set fuzz percentage
image.ColorFuzz = new ImageMagick.Percentage(fuzz);
//trim borders
image.Trim();
//resize image to original size
MagickGeometry size = new MagickGeometry(width, height);
size.IgnoreAspectRatio = true;
image.Resize(size);
// Save the result
image.Write(destImageFullPath);
}
在下图中,您可以在左侧看到原始图像,在右侧看到调整大小后的图像:
注释
Trim
删除与图像角落像素颜色相同的任何边框(有关详细信息,请参阅)Fuzz
删除“相似”颜色(详细信息)。如前所述,如果您的边框是透明的,只需离开fuzz=0
那么你的图像中的黄色区域实际上不是黄色的,而是透明的?那么你的图像中的黄色区域实际上不是黄色的,而是透明的?