簡體   English   中英

使用 VisualBrush 獲取 WPF 區域的 System.Drawing.Bitmap

[英]Get System.Drawing.Bitmap of a WPF Area using VisualBrush

關鍵是,我需要轉換為 System.Drawing.Bitmap (.Net Framework 2.0) 以獲得 WPF 網格的單幀及其內容。

我閱讀了有關VisualBrushDrawingBrush的信息,但我無法想象它應該如何工作。

我可以成功地將任何 WPF BitmapSource轉換為我的System.Drawing.Bitmap 但是如何從我的網格接收BitmapSource

謝謝

要將Visual轉換為BitmapSource ,您可以使用RenderTargetBitmapVisualBrushDrawingVisual

public BitmapSource ConvertToBitmapSource(UIElement element)
{
    var target = new RenderTargetBitmap((int)(element.RenderSize.Width), (int)(element.RenderSize.Height), 96, 96, PixelFormats.Pbgra32);
    var brush = new VisualBrush(element);

    var visual = new DrawingVisual();
    var drawingContext = visual.RenderOpen();


    drawingContext.DrawRectangle(brush, null, new Rect(new Point(0, 0),
        new Point(element.RenderSize.Width, element.RenderSize.Height)));

    drawingContext.Close();

    target.Render(visual);

    return target;
}   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM