我想在不创建更多资源的情况下更改从资源文件创建的图像的颜色。我目前的解决方案是使用我想要的颜色创建第二个图像资源。但我想在我的代码中设置它。我像这样加载图像:
var icon =
BitmapSourceFromBrush.Convert32(
(DrawingBrush) Application.Current.FindResource("IconNoframeSettings"));
我可以设置一个新的单色吗?在 Convert32 中,将其设置为 DrawingBrush 还是其他方式?
这是 BitmapSourceFromBrush (感谢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;
}