3

这是一个由两部分组成的问题——首先,为什么这段代码不起作用?

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。画布是在代码中使用形状(折线等)绘制的。然后我需要将此 BitmapSource 以大约每秒 60 帧的速率传递给 xaml 中的图像。我注意到如果我创建一个模拟 BitmapSource,Image.Source 正在使用 CachedBitmap,但是每次我更新我的(黑色)位图时它都会重新绑定到我的 BitmapImage。

关于如何以 60fps 在内存中创建 Canvas 并从中创建一个被 Image.Source 视为 CachedBitmap 的 BitmapSource 的建议?

4

2 回答 2

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;  
}
于 2012-11-15T02:51:13.037 回答
0

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

bmp.BeginInit();
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.StreamSource = outStream;
bmp.EndInit();
于 2014-07-02T20:14:34.157 回答