title: C# WPF - Problem changing color of image in realtime
body: <p>I'm having some problems changing the color of images in realtime. My vision is that the user of my main project should be able to adjust the colors by adjusting 3 sliders for RGB channels.</p>
  <p>I found a good algorithm that does the job perfectly but the problem I'm facing now is that the color will only change once in the application. But on disk I can see the color changing.</p>
  <p>Another potential problem is that saving the image to disk each time the color changes could be a performance issue (especially when dragging sliders in a quick manner), so I was hoping it was possible to load the converted image to a control like a button directly from the objec/memory.</p>
  <p>I have set up a sample application for demonstration purposes. In the application I only change the Red channel by entering a value in a textbox. The other channels have been predefined in the code.</p>
  <p>Any help with with this is highly appreciated since I've been struggling with this task for almost a week now.</p>
  <p>First value with 0 on Red channel shows fine
  <a href="https://i.stack.imgur.com/Xsd7A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Xsd7A.png" alt="First value with 0 on Red channel shows fine" /></a></p>
  <p>Second value with 255 should have changed the image to yellow color but does not
  <a href="https://i.stack.imgur.com/1VVtH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1VVtH.png" alt="Second value with 255 should have changed the image to yellow color but does not" /></a></p>
  <p>Image is updated when I have it opened in the Photos app
  <a href="https://i.stack.imgur.com/xjqOk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xjqOk.png" alt="Image is updated when I have it opened in the Photos app" /></a></p>
  <pre><code>using System;
  using System.Windows;
  using Point = System.Drawing.Point;
  using System.Drawing;
  using System.Drawing.Imaging;
  using Image = System.Drawing.Image;
  using System.Windows.Media.Imaging;
  using Rectangle = System.Drawing.Rectangle;
  using System.Threading;
  
  namespace ImageColorConversionTesting
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
  
      public partial class MainWindow : Window
      {
  
          public MainWindow()
          {
              InitializeComponent();
  
          }
  
          private void Button1_Click(object sender, RoutedEventArgs e)
          {
              byte number;
  
              if(System.Byte.TryParse(NumberTextBox.Text, out number))
              {
  
                  LoadImage(number);
  
              }
  
          }
  
          private void LoadImage(byte redcolor)
          {
              Bitmap newImage = ConvertImageColor(255, redcolor, 255, 0, @&quot;Images\house-64.png&quot;);
              newImage.Save(&quot;test.png&quot;);
              System.Windows.Controls.Image imgHome = new System.Windows.Controls.Image();
              imgHome.Source = BitmapFromUri(new Uri(&quot;test.png&quot;, UriKind.Relative));
             
              image3.Source = imgHome.Source;
  
          }
  
          public Bitmap ConvertImageColor(int alpha, int red, int green, int blue, string filename)
          {
              System.Drawing.Color newColor = System.Drawing.Color.FromArgb(alpha, red, green, blue);
              Image originalImage = Image.FromFile(filename);
              Bitmap newImage = ToColorTone(originalImage, newColor);
  
              return newImage;
  
          }
  
          private Bitmap ToColorTone(Image image, System.Drawing.Color color)
          {
  
              int brightness = color.A;
              float scale = brightness;
  
              float r = color.R / 255f * scale;
              float g = color.G / 255f * scale;
              float b = color.B / 255f * scale;
  
     
              ColorMatrix cm = new ColorMatrix(new float[][]
              {
                  new float[] {r, 0, 0, 0, 0},
                  new float[] {0, g, 0, 0, 0},
                  new float[] {0, 0, b, 0, 0},
                  new float[] {0, 0, 0, 1, 0},
                  new float[] {0, 0, 0, 0, 1}
              });
  
              ImageAttributes ImAttribute = new ImageAttributes();
              ImAttribute.SetColorMatrix(cm);
  
            
              Point[] points =
              {
                  new Point(0, 0),
                  new Point(image.Width - 1, 0),
                  new Point(0, image.Height - 1),
              };
              Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
  
              Bitmap myBitmap = new Bitmap(image.Width, image.Height);
              using(Graphics graphics = Graphics.FromImage(myBitmap))
              {
                  graphics.DrawImage(image, points, rect, GraphicsUnit.Pixel, ImAttribute);
              }
              return myBitmap;
          }
  
          public static System.Windows.Media.ImageSource BitmapFromUri(Uri source)
          {
              var bitmap = new BitmapImage();
              bitmap.BeginInit();
              bitmap.UriSource = source;
              bitmap.CacheOption = BitmapCacheOption.OnLoad;
              bitmap.EndInit();
              return bitmap;
          }
  
      }
  }
  </code></pre>