我想更改从我的资源文件创建的图像的颜色,而不创建更多资源。 我目前的解决方案是使用我想要的颜色创建第二个图像资源。但我想在我的代码中设置它。 我加载这样的图像:
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;
}