我一直在试验一个名为 ImageSharp 的图像处理器,因为 System.Drawing 在 asp.net 核心上不可用,而且 System.Drawing 可能会遇到麻烦。
我想用图像中的特定纹理填充空白。下面的代码正在运行,但速度非常慢。
因为这是我第一次处理图像,所以我真的不知道最有效的方法是什么。
用纹理填充空白的最佳和有效方法是什么。
结果: 甜甜圈
为此: 闪闪发光的甜甜圈
public void CreateImage()
{
var webRoot = _env.WebRootPath;
var ImgSrc = "\\Images\\SampleImage.png";
var TextureURL = "\\Images\\Starsinthesky.jpg";
var file = webRoot + ImgSrc;
var texture = webRoot + TextureURL;
var myPath = Path.Combine(webRoot, ImgSrc);
byte[] img;
using (Image<Rgba32> image = Image.Load(file))
{
HashSet<Texture> textureArr = getRGBaBytes(texture, image);
for (int h = 0; h <= image.Height; h++)
{
for(int w = 0; w <= image.Width; w++)
{
if(image[w,h] == Rgba32.FromHex("#ffffff"))
{
image[w, h] = textureArr.Where(t => t.x == w && t.y == h).First().color;
}
}
}
image.Save("NewImage.png");
}
}
public HashSet<Texture> getRGBaBytes(string textureURL, Image<Rgba32> sample)
{
using (Image<Rgba32> tex = Image.Load(textureURL))
{
int bitsizelimit = int.MaxValue;
if (sample.Width > tex.Width || sample.Height > tex.Height)
{
throw new Exception("Texture image dimensions must be greater or equal to sample image");
}
HashSet<Texture> myTexture = new HashSet<Texture>();
for (int h = 0; h <= sample.Height; h++)
{
for (int w = 0; w <= sample.Width; w++)
{
System.Diagnostics.Debug.WriteLine($"{tex[w,h].ToHex()} at x:{w} y:{h}");
myTexture.Add(new Texture { color = tex[w, h], x = w, y = h });
}
}
return myTexture;
}
}
public class Texture
{
public Rgba32 color { get; set; }
public int x { get; set; }
public int y { get; set; }
}