2017-01-21 18 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に

おかげで、彼は正しい答えを提供します。しかしグリッチで、ここに私のために働いた最終版です:

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

次のようにChangeToColor機能ですが:

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ありがとうございます。 – Lamar

+0

投票が取り消されました:) – MickyD

+0

または[こちらをご覧ください](http://stackoverflow.com/questions/28847270/c-sharp-recolored-image-pixelated/28849281?s=9|0.0530#28849281)! – 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; 
    } 

は、作成したビットマップが漏れないようにしてくださいよ!

他の方法もあります。 Hereは、ColorMappingを使用する方法へのリンクです。これにより、色の範囲を他の範囲に置き換えることができるので、アンチアルティスグラフィックスのようなグラデーションを維持できます。

+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

関連する問題