简体   繁体   English

如何将 DrawingVisual 转换为位图?

[英]How do I convert DrawingVisual to a Bitmap?

Using Visual C# 2010, I'm trying to write an .avi file from frames received from a Windows Kinect.使用 Visual C# 2010,我试图从从 Windows Kinect 接收的帧中编写 .avi 文件。 The frames can be saved easily enough as .png files with the use of a BitmapEncoder and PngBitmapEncoder (saving to a stream) but I can't add these images at my discretion to a VideoStream provided here: http://www.codeproject.com/Articles/7388/A-Simple-C-Wrapper-for-the-AviFile-Library because I need to be able to convert either a RenderTargetBitmap or a DrawingVisual to a System.Drawing.Bitmap.使用 BitmapEncoder 和 PngBitmapEncoder(保存到流)可以很容易地将帧保存为 .png 文件,但我无法自行决定将这些图像添加到此处提供的 VideoStream: http://www.codeproject。 com/Articles/7388/A-Simple-C-Wrapper-for-the-AviFile-Library因为我需要能够将 RenderTargetBitmap 或DrawingVisual转换为 System.Drawing.Bitmap。

I've found example codes that do similar things but they all seem to want to instantiate the Image class which Visual Studio tells me is abstract and can't be instantiated.我找到了做类似事情的示例代码,但它们似乎都想实例化 Visual Studio 告诉我是抽象的并且无法实例化的 Image 类。

I'm going round in circles and not getting anywhere.我绕着圈子走,哪儿也去不了。

I just want to do something like this:我只想做这样的事情:

...
renderBitmap.Render(dv);
Bitmap bmp=new Bitmap(dv);
VideoStream aviStream=aviManager.AddVideoStream(true,60,bmp);
...

But Bitmap has no useful constructors to get me from dv (DrawingVisual) to bmp.但是 Bitmap 没有有用的构造函数来让我从 dv (DrawingVisual) 到 bmp。 :( :(

Those 3 lines come from this snippet:这 3 行来自这个片段:

var renderBitmap=new RenderTargetBitmap(colorWidth,colorHeight,96.0,96.0,PixelFormats.Pbgra32);
DrawingVisual dv=new DrawingVisual();
using(DrawingContext dc=dv.RenderOpen())
{
    VisualBrush backdropBrush=new VisualBrush(Backdrop);
    dc.DrawRectangle(backdropBrush,null,new Rect(0,0,colorWidth,colorHeight));
    VisualBrush colorBrush=new VisualBrush(MaskedColor);
    dc.DrawRectangle(colorBrush,null,new Rect(0,0,colorWidth,colorHeight));
    VisualBrush watermarkBrush=new VisualBrush(Watermark);
    dc.DrawRectangle(watermarkBrush,null,new Rect(colorWidth-96,colorHeight-80,64,48));
}
renderBitmap.Render(dv);
Bitmap bmp=new Bitmap(dv);
VideoStream aviStream=aviManager.AddVideoStream(true,60,bmp);

The result of using RenderTargetBitMap is a WPF BitMapSource it doesn't convert the Visual itself to a BitmapSource , it contains the result of the conversion as a BitmapSource .使用RenderTargetBitMap的结果是 WPF BitMapSource它不会将 Visual 本身转换为BitmapSource ,它包含转换结果作为BitmapSource In order to convert a BitmapSource to a System.Drawing.Bitmap try using a modified version of the code from this MSDN Forum Post .为了将BitmapSource转换为System.Drawing.Bitmap尝试使用此MSDN 论坛帖子中的代码的修改版本。

renderBitmap.Render(dv);
BitmapSource bmp = renderBitmap;

using(MemoryStream outStream = new MemoryStream())
{
    BitmapEncoder enc = new BmpBitmapEncoder();
    enc.Frames.Add(BitmapFrame.Create(bmp));
    enc.Save(outStream);
    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
    VideoStream aviStream=aviManager.AddVideoStream(true,60,bitmap);  
}

Created a Method to return your Bitmap创建了一个方法来返回您的位图

renderBitmap.Render(dv);
BitmapSource bmp =renderBitmap;

VideoStream aviStream = aviManager.AddVideoStream(true, 60, ConvertToBitmap(bmp));

private System.Drawing.Bitmap ConvertToBitmap(BitmapSource target)
{
    System.Drawing.Bitmap bitmap;

    using (MemoryStream outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(target));
        enc.Save(outStream);
        bitmap = new System.Drawing.Bitmap(outStream);
    }

    return bitmap;
}
private BitmapSource ToBitmapSource(Visual visual, Brush transparentBackground)
{
    var bounds = VisualTreeHelper.GetDescendantBounds(visual);
    var scale = VisualTreeHelper.GetDpi(visual);
    var bitmapSource = new RenderTargetBitmap(
        (int)(bounds.Width * scale.DpiScaleX),
        (int)(bounds.Height * scale.DpiScaleY),
        scale.PixelsPerInchX,
        scale.PixelsPerInchY,
        PixelFormats.Pbgra32);
    var drawingVisual = new DrawingVisual();
    using (var drawingContext = drawingVisual.RenderOpen())
    {
        drawingContext.DrawRectangle(transparentBackground, null, new Rect(bounds.Size));
        drawingContext.DrawRectangle(new VisualBrush(visual), null, new Rect(bounds.Size));
    }
    bitmapSource.Render(drawingVisual);
    return bitmapSource;
}

private System.Drawing.Bitmap BitmapFromSource(BitmapSource bitmapsource)
{
    System.Drawing.Bitmap bitmap;
    using (MemoryStream outStream = new MemoryStream())
    {
        BitmapEncoder enc = new BmpBitmapEncoder();
        enc.Frames.Add(BitmapFrame.Create(bitmapsource));
        enc.Save(outStream);
        bitmap = new System.Drawing.Bitmap(outStream);
    }
    return bitmap;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM