将WPF控件转换为BitmapSource

时间:2011-07-14 03:46:02

标签: wpf bitmapimage bitmapsource rendertargetbitmap

这是一个两部分问题 - 首先,为什么这段代码不起作用?

Canvas canvas = new Canvas { Width = 640, Height = 480 };
System.Windows.Size size = new System.Windows.Size( canvas.Width, canvas.Height);

//Measure and arrange the surface
canvas.Measure( size );
canvas.Arrange( new Rect( size ) );

canvas.Background = new SolidColorBrush( Colors.Purple );

RenderTargetBitmap bitmap = new RenderTargetBitmap( (int)canvas.Width, (int)canvas.Height, 96d, 96d, PixelFormats.Pbgra32 );
bitmap.Render( canvas );
BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add( BitmapFrame.Create( bitmap ) );

using ( MemoryStream outStream = new MemoryStream() )
{
    encoder.Save( outStream );

    outStream.Seek( 0, SeekOrigin.Begin );
    BitmapImage bmp = new BitmapImage { CacheOption = BitmapCacheOption.OnLoad };
    bmp.BeginInit();
    bmp.StreamSource = outStream;
    bmp.EndInit();
}

当我把图像写入磁盘时,我看到的只是一张黑色图像 - 我以前做过这个并且没有任何问题,但是现在有些东西逃脱了我...我已经检查了宽度和高度以及缓冲区MemoryStream中的数据,一切看起来还不错......

这只是一个测试,真正的目标是从Canvas视觉图像创建一个BitmapSource。 Canvas在代码中使用Shapes(折线等)绘制。然后,我需要以每秒约60帧的速率将此BitmapSource传递给xaml中的Image。我注意到如果我创建一个模拟BitmapSource,Image.Source正在使用CachedBitmap,但每次更新我的(黑色)位图时它都会重新绑定到我的BitmapImage。

有关如何在内存中以60fps创建Canvas并从Image.Source将其视为CachedBitmap创建BitmapSource的建议?

2 个答案:

答案 0 :(得分:0)

Makubex是正确的 - 你需要等到事物被加载之后,视觉效果处于他们实际上已经呈现任何能够复制的东西的状态;那说,虽然我不在安装了Studio的计算机上,但我安装了LINQPad ....

void Main()
{   
    mainWindow = new Window(){ Width = 640, Height = 480, Title = "Main Window" };
    canvas = new Canvas { Width = 640, Height = 480 };
    System.Windows.Size size = new System.Windows.Size( canvas.Width, canvas.Height );
    // Measure and arrange the surface
    canvas.Measure( size );
    canvas.Arrange( new Rect( size ) );
    canvas.Background = new SolidColorBrush( Colors.Purple );   

    mirrorTimer = new DispatcherTimer(
        TimeSpan.FromMilliseconds(1000.0 / 60.0), 
        DispatcherPriority.Background, 
        CopyToMirror, 
        Dispatcher.CurrentDispatcher);
    updateTimer = new DispatcherTimer(
        TimeSpan.FromMilliseconds(1000.0 / 60.0), 
        DispatcherPriority.Background, 
        DrawSomething, 
        Dispatcher.CurrentDispatcher);

    mainWindow.Loaded += 
        (o,e) => 
        {
            mirrorWindow = new Window { Width = 640, Height = 480, Title = "Mirror Window" };
            mirrorWindow.Show();
            mirrorWindow.Loaded += 
                (o2,e2) => 
                { 
                    mirrorTimer.Start(); 
                };          
        };
    mainWindow.Closed += 
        (o,e) => 
        { 
            if(mirrorTimer != null) 
            {
                mirrorTimer.Stop();
                mirrorWindow.Close();
            }
        };
    mainWindow.Content = canvas;
    mainWindow.Show();  
}

Window mainWindow;
Window mirrorWindow;
Canvas canvas;
DispatcherTimer mirrorTimer;
DispatcherTimer updateTimer;
Random rnd = new Random();

private void DrawSomething(object sender, EventArgs args)
{
    canvas.Children.Clear();
    canvas.Background = Brushes.White;
    var blob = new Ellipse() { Width = rnd.Next(0, 20), Height = rnd.Next(0, 20) };
    blob.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)rnd.Next(0,255), (byte)rnd.Next(0,255), (byte)rnd.Next(0,255)));
    Canvas.SetLeft(blob, (int)rnd.Next(0, (int)canvas.ActualWidth));
    Canvas.SetTop(blob, (int)rnd.Next(0, (int)canvas.ActualHeight));
    canvas.Children.Add(blob);
}

private void CopyToMirror(object sender, EventArgs args)
{       
    var currentImage = (mirrorWindow.Content as Image);
    if(currentImage == null)
    {
        currentImage = new Image(){ Width = 640, Height = 480 };
        mirrorWindow.Content = currentImage;
    }

    RenderTargetBitmap bitmap = new RenderTargetBitmap( (int)canvas.Width, (int)canvas.Height, 96d, 96d, PixelFormats.Pbgra32 );
    bitmap.Render( canvas );
    BitmapEncoder encoder = new BmpBitmapEncoder();
    encoder.Frames.Add( BitmapFrame.Create( bitmap ) );

    BitmapImage bmp = new BitmapImage() { CacheOption = BitmapCacheOption.OnLoad };
    MemoryStream outStream = new MemoryStream();
    encoder.Save(outStream);
    outStream.Seek(0, SeekOrigin.Begin);
    bmp.BeginInit();
    bmp.StreamSource = outStream;
    bmp.EndInit();      
    currentImage.Source = bmp;  
}

答案 1 :(得分:0)

如果您仍然遇到黑色图片问题,请将您的CacheOption设置为在BeingInit调用之后:

bmp.BeginInit();
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.StreamSource = outStream;
bmp.EndInit();
相关问题