我有一个带有某些矢量图形的DrawingBrush。我想将其转换为BitmapSource,作为将其转换为Bitmap的中间步骤。最好的方法是什么?
最佳答案
public static BitmapSource BitmapSourceFromBrush(Brush 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;
}
关于wpf - 如何从画笔(例如DrawingBrush)转换为BitmapSource?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7038079/