3

Visual C# 2010 を使用して、Windows Kinect から受信したフレームから .avi ファイルを書き込もうとしています。フレームは、BitmapEncoder と PngBitmapEncoder (ストリームに保存) を使用して .png ファイルとして簡単に保存できますが、これらの画像をここで提供される VideoStream に自分の裁量で追加することはできません: http://www.codeproject. RenderTargetBitmap または DrawingVisual を System.Drawing.Bitmap に変換できるようにする必要があるためです。

同様のことを行うサンプルコードを見つけましたが、それらはすべて、Visual Studio が抽象的でインスタンス化できないと言った Image クラスをインスタンス化したいようです。

私はぐるぐる回っていて、どこにも行きません。

私はちょうどこのようなことをしたい:

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

しかし、Bitmap には、dv (DrawingVisual) から bmp に移行するための便利なコンストラクターがありません。:(

これらの 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);
4

2 に答える 2

2

RenderTargetBitMapを使用した結果は、ビジュアル自体をに変換しないWPF BitMapSourceBitmapSourceであり、変換の結果をとして含みますBitmapSourceこのMSDNフォーラム投稿のコードの修正バージョンを使用しBitmapSourceてを試してみるために。System.Drawing.Bitmap

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

ビットマップを返すメソッドを作成しました

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;
}
于 2012-09-01T16:55:34.757 に答える
0
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;
}
于 2020-05-10T11:49:04.637 に答える