开发者

How do I convert from a Brush (e.g. DrawingBrush) to a BitmapSource?

开发者 https://www.devze.com 2023-03-27 09:44 出处:网络
I have a DrawingBrush with some vector graphics. I want to convert it to Bitm开发者_运维知识库apSource as an intermediate step to getting it to Bitmap. What\'s the (best) way to do this?public static

I have a DrawingBrush with some vector graphics. I want to convert it to Bitm开发者_运维知识库apSource as an intermediate step to getting it to Bitmap. What's the (best) way to do this?


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消