title: Change image color with transparent background
body: <p>I need to load an image with green circle over a transparent background into a bitmap image using c# (System.Drawings).</p>
  
  <p>That's the easy part. However I need to change the color of the circle before adding it to the bigger image, without affecting the transparency of the surrounding. In my case I need to change the circle color to yellow and add it as a sun.</p>
  
  <p>I can't use fixed yellow circle image because the desired color is dynamic.</p>
  
  <p>So in the code below, how can I change the color of the image before adding it to the bitmap?</p>
  
  <pre><code>Image i = Image.FromFile(greenCircleFile);
  Bitmap b = new Bitmap(500, 500);
  
  using(Graphics g = Graphics.FromImage(b))
  {
      //--&gt; 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);
  }
  </code></pre>
  
  <p>Please note that two things need to be into consideration: Keeping the anti-aliasing of the shape (circle), and the color needs to be picked by user and used as is to overlay the original color of the circle.</p>
  
  <p><strong>Fixed:</strong> </p>
  
  <p>Thanks to @TaW, he provided the correct answer. However with a glitch, here's the final version that worked for me:</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>While ChangeToColor function is as follows:</p>
  
  <pre><code>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;
  }
  </code></pre>