在 System.Drawing.Bitmap 中转换 canvas 的背景?

Convert a background of a canvas in System.Drawing.Bitmap?

如何在System.Drawing.Bitmap中转换CANVAS的背景? canvas 图像是kinect图像的背景。该应用程序是在 WPF 中创建的。

我在 canvas 上创建了一个矩形并保存它,您可以根据您的可用性相应地更改代码

using (Graphics g = Graphics.FromImage(image))
{
    g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;

    foreach (var child in canvas.Children)
    {
        if (child is System.Windows.Shapes.Rectangle)
        {
            var oldRect = child as System.Windows.Shapes.Rectangle;

            // need to do something here to make the new rect bigger as the scale is clearly different
            var rect = new Rectangle((int)Canvas.GetLeft(oldRect), (int)Canvas.GetTop(oldRect), (int)oldRect.Width, (int)oldRect.Height);
            g.FillRectangle(Brushes.Black, rect);
        }
    }
    RenderTargetBitmap bmp = new RenderTargetBitmap((int)Canvas1.Width, (int)Canvas1.Height, 96, 96, PixelFormats.Default);
    bmp.Render(Canvas1);
}