ImageSharp라는 이미지 프로세서를 사용해 본 적이 있는데, asp.net 코어에서 System.Drawing을 사용할 수 없으며 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")'행을 이중 중첩 루프 밖으로 승격시키는 것입니다.최적화 프로그램은 상수이고 한 번만 생성을 수행하지만 그렇지 않은 경우에는 이미지의 모든 픽셀에 대해 동일한 색상을 생성하므로 흰색인지 여부를 확인할 수 있습니다. – pstrjds