【问题标题】:How do I convert from a Brush (e.g. DrawingBrush) to a BitmapSource?如何从画笔(例如 DrawingBrush)转换为 BitmapSource?
【发布时间】:2011-08-12 09:23:20
【问题描述】:

我有一个带有一些矢量图形的绘图笔。我想将它转换为 BitmapSource 作为将其转换为 Bitmap 的中间步骤。这样做的(最佳)方法是什么?

【问题讨论】:

    标签: wpf bitmapsource drawingbrush


    【解决方案1】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 2017-08-19
      • 1970-01-01
      • 2022-01-15
      相关资源
      最近更新 更多