gpt4 book ai didi

wpf - 将 WPF 控件转换为 BitmapSource

转载 作者:行者123 更新时间:2023-12-02 12:26:25 28 4
gpt4 key购买 nike

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

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

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

最佳答案

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

关于wpf - 将 WPF 控件转换为 BitmapSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6688262/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com