투명 배경 위에 녹색 원으로 이미지를로드하려면 C# (System.Drawings)을 사용하여 비트 맵 이미지로로드해야합니다.투명 배경으로 이미지 색상 변경
그건 쉬운 부분입니다. 그러나 주위의 투명도에 영향을주지 않고 더 큰 이미지에 추가하기 전에 원의 색을 변경해야합니다. 내 경우에는 원색을 노란색으로 변경하고 태양으로 추가해야합니다.
원하는 색상이 동적이기 때문에 고정 된 노란색 원 화상을 사용할 수 없습니다.
그래서 아래 코드에서 비트 맵에 이미지를 추가하기 전에 이미지의 색을 어떻게 바꿀 수 있습니까?
Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);
using(Graphics g = Graphics.FromImage(b))
{
//--> Here I need to change the color of the green circle to yellow
//afterwards I can add it to the bitmap image
g.DrawImage(i, 0, 0, 500, 500);
}
두 가지가 고려 될 필요가 있습니다 : 모양 (원)의 앤티 앨리어싱을 유지하고의 원래 색상을 오버레이와 같이 색상이 사용자에 의해 포착하고 사용 할 필요가 원. 고정
: @TaW에
덕분에, 그는 정답을 제공했다.
가 ChangeToColor 기능은 다음과됩니다Image i = Image.FromFile(greenCircleFile);
Bitmap b = new Bitmap(500, 500);
using(Graphics g = Graphics.FromImage(b))
{
//Here I need to change the color of the green circle to yellow
i = ChangeToColor(b, Color.Gold)
//afterwards I can add it to the bitmap image
g.DrawImage(i, 0, 0, 500, 500);
}
로 동안 : 그러나 글리치와 함께, 여기에 나를 위해 일한 최종 버전의
이Bitmap ChangeToColor(Bitmap bmp, Color c)
{
Bitmap bmp2 = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(bmp2))
{
float tr = c.R/255f;
float tg = c.G/255f;
float tb = c.B/255f;
ColorMatrix colorMatrix = new ColorMatrix(new float[][]
{
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {tr, tg, tb, 0, 1}
});
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(colorMatrix);
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height),
0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attributes);
}
return bmp2;
}
샘플 코드가 추가되었습니다. – Lamar
@ 라마르 감사합니다. 투표가 취소되었습니다 :) – MickyD
[또는 여기를 참조하십시오 (http://stackoverflow.com/questions/28847270/c-sharp-recolored-image-pixelated/28849281?s=9|0.030302882828)! – TaW