2

I want to change the color on my Images created from my resource file without creating more resources. My current solution is to create a second image resource with the color i want. But i want to set it in my code. I load the image like this:

    var icon =
       BitmapSourceFromBrush.Convert32(
          (DrawingBrush) Application.Current.FindResource("IconNoframeSettings"));

Can i set a new single color? Either in the Convert32, set it to the DrawingBrush or some other way?

Here is the BitmapSourceFromBrush (Credits to Tim Lovell-Smith):

    public static BitmapSource Convert32(DrawingBrush drawingBrush, int size = 32, int dpi = 96)
    {
        // RenderTargetBitmap = builds a bitmap rendering of a visual
        var pixelFormat = PixelFormats.Pbgra32;
        RenderTargetBitmap rtb = new RenderTargetBitmap(size, size, dpi, dpi, pixelFormat);

        // Drawing visual allows us to compose graphic drawing parts into a visual to render
        var drawingVisual = new DrawingVisual();
        using (DrawingContext context = drawingVisual.RenderOpen())
        {
            // Declaring drawing a rectangle using the input brush to fill up the visual
            context.DrawRectangle(drawingBrush, null, new Rect(0, 0, size, size));
        }

        // Actually rendering the bitmap
        rtb.Render(drawingVisual);
        return rtb;
    }
Community
  • 1
  • 1
ace
  • 21
  • 3

0 Answers0