私はImageSharpというイメージプロセッサを試していました。なぜなら、System.Drawingはasp.netコアでは使用できず、System.Drawingには不具合があります。asp.netコアのテクスチャと色を入れ替えるC#
空白を画像の特定のテクスチャで塗りつぶしたい。以下のコードは動作していますが、痛いほど遅いです。
これは初めて画像を処理するため、これを行う最も効率的な方法が実際にはわかりません。
空白にテクスチャを塗りつぶすには、どのような方法が最適です。これに Doughnut
:この結果の
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; }
}
は、あなたは基本的にはすでになどの使用しています。それはあなたのパフォーマンスを助けるはずです。私はテストプログラムを作ってこれをテストしようとしていましたが、LinqPadで実行することはできません。 – pstrjds
もう一つは、この行 'Rgba32.FromHex("#ffffff ")'を二重にネストされたループの外側に進めることです。オプティマイザはそれが定数であることに気づき、作成を1回しか実行しませんが、そうでない場合、イメージのすべてのピクセルに同じ色を作成しているので、白であるかどうかを確認できます。 – pstrjds