2017-01-21 1 views
3

투명 배경 위에 녹색 원으로 이미지를로드하려면 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; 
} 
+1

샘플 코드가 추가되었습니다. – Lamar

+0

@ 라마르 감사합니다. 투표가 취소되었습니다 :) – MickyD

+0

[또는 여기를 참조하십시오 (http://stackoverflow.com/questions/28847270/c-sharp-recolored-image-pixelated/28849281?s=9|0.030302882828)! – TaW

답변

3

이 새로운 색상으로보기 모든 불투명 픽셀로 새로운 비트 맵 이동 만듭니다 :

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} // kudos to OP! 
       }); 

      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; 
    } 

는 비트 맵이 생성 누설하지 않도록해야합니까!

다른 방법도 있습니다. HereColorMapping을 사용하는 방법에 대한 링크입니다. 이렇게하면 다른 범위로 대체 할 수 있으므로 반올림 된 그래픽과 같은 그라디언트를 유지할 수 있습니다.

+0

고마워! 나는 당신의 stackoverflow 참조를 다음과 즐겼다. 그러나 "Color.Gold"를 빨간색 이미지에 적용했지만 또 다른 빨간색 음영을 생성했습니다. RGB에서는 이해하지만 매트릭스에있는 다른 WA 배열은 이해하지 못합니다. – Lamar

+0

지금 작업했습니다. 내 업데이트를 확인하십시오. 고마워요! – Lamar

+0

고마워요 !! 실제로 제 버전에서 작동하는 색상으로 테스트했지만 코드가 실제로 옳습니다. 나는 나의 대답을 업데이트했다. – TaW

-2

여기 내 솔루션입니다 방금 새로운 컨트롤을 만들 필요는

그런 다음 Picturebox를 상속 받아이를 확인하십시오.

public partial class UICirclePicture : PictureBox 
{ 
    [Browsable(false)] 
    public int Depth { get; set; } 
    [Browsable(false)] 
    public SprikiwikiUI Ui 
    { 
     get { return SprikiwikiUI.Instance; } 
    } 
    [Browsable(false)] 
    public MouseState MouseState { get; set; } 

    public UICirclePicture() 
    { 


     BackColor = Ui.GetApplicationBackgroundColor(); 
     SizeMode = PictureBoxSizeMode.StretchImage; 

    } 


    protected override void OnResize(EventArgs e) 
    { 
     base.OnResize(e); 
     using (var gp = new GraphicsPath()) 
     { 
      gp.AddEllipse(new Rectangle(0, 0, this.Width - 1, this.Height - 1)); 
      this.Region = new Region(gp); 
     } 
    } 

} 
+0

OP는 벡터 모양이 아닌 래스터 이미지를 그리려합니다. 또한,'OnResize'의 그림은 다소 이상합니다. – MickyD

+0

사용자 인터페이스 나 컨트롤이 없습니다. 이미지 편집 만하면됩니다. – Lamar

+0

@ 라마르 나는 내가 도울 수 없다는 것을 안다. .. 미안. – mardagz