C# 在 WPF GUI 上显示新接收的帧
C# Displaying new received Frame on a WPF GUI
我目前正在开发一个客户端,它以字节数组的形式从服务器接收帧。这些字节数组将转换为 BitmapSource。
然而,当我尝试在我的 WPF GUI 上显示到达的帧时,似乎没有任何反应。
这是接收字节数组,转换它并在 GUI 上显示它的方法。
public void runSocketRoutine(){
while (true){
byte[] content = new byte[8294400];
Console.WriteLine("Waiting for Messages.");
using (ZMessage message = subscriber.ReceiveMessage()){
Console.WriteLine("Message received!");
string pubID = message[0].ReadString();
/*receive bytearray from publisher*/
content = message[1].Read();
Console.WriteLine("size of content: " + message[1].Length);
/*create BitmapSource out of the bytearray you receive.*/
BitmapSource source = BitmapSource.Create(1920, 1080, 72, 72, PixelFormats.Bgra32, BitmapPalettes.Gray256, content, 1920 * 4);
if (source != null){
Console.WriteLine("source is created");
}
/*display image on screen*/
videoView.Source = source;
Console.WriteLine("videoView updated");
}
}
}
这是我的 xaml 内容。
<Window x:Class="FrameByteArrayTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FrameByteArrayTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Viewbox Stretch="Uniform">
<Image x:Name="imgView" Stretch="UniformToFill">
</Image>
</Viewbox>
</Grid>
我什至想在 GUI 上显示一个选择的 png,方法是创建一个 BitmapImage,该 BitmapImage 由具有给定数据路径的 URI 对象传递。好像我不在主方法中显示任何图片都无法显示。
我将不胜感激每一个提示。
许多问候
有些事情告诉我你没有在你的 UI 线程上做 I/O。试试这个:
using (ZMessage message = subscriber.ReceiveMessage()){
Console.WriteLine("Message received!");
string pubID = message[0].ReadString();
/*receive bytearray from publisher*/
content = message[1].Read();
Console.WriteLine("size of content: " + message[1].Length);
/*create BitmapSource out of the bytearray you receive.*/
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
BitmapSource source = BitmapSource.Create(1920, 1080, 72, 72,
PixelFormats.Bgra32, BitmapPalettes.Gray256, content, 1920 * 4);
if (source != null){
Console.WriteLine("source is created");
}
/*display image on screen*/
videoView.Source = source;
Console.WriteLine("videoView updated");
}));
}
我目前正在开发一个客户端,它以字节数组的形式从服务器接收帧。这些字节数组将转换为 BitmapSource。 然而,当我尝试在我的 WPF GUI 上显示到达的帧时,似乎没有任何反应。
这是接收字节数组,转换它并在 GUI 上显示它的方法。
public void runSocketRoutine(){
while (true){
byte[] content = new byte[8294400];
Console.WriteLine("Waiting for Messages.");
using (ZMessage message = subscriber.ReceiveMessage()){
Console.WriteLine("Message received!");
string pubID = message[0].ReadString();
/*receive bytearray from publisher*/
content = message[1].Read();
Console.WriteLine("size of content: " + message[1].Length);
/*create BitmapSource out of the bytearray you receive.*/
BitmapSource source = BitmapSource.Create(1920, 1080, 72, 72, PixelFormats.Bgra32, BitmapPalettes.Gray256, content, 1920 * 4);
if (source != null){
Console.WriteLine("source is created");
}
/*display image on screen*/
videoView.Source = source;
Console.WriteLine("videoView updated");
}
}
}
这是我的 xaml 内容。
<Window x:Class="FrameByteArrayTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FrameByteArrayTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Viewbox Stretch="Uniform">
<Image x:Name="imgView" Stretch="UniformToFill">
</Image>
</Viewbox>
</Grid>
我什至想在 GUI 上显示一个选择的 png,方法是创建一个 BitmapImage,该 BitmapImage 由具有给定数据路径的 URI 对象传递。好像我不在主方法中显示任何图片都无法显示。
我将不胜感激每一个提示。 许多问候
有些事情告诉我你没有在你的 UI 线程上做 I/O。试试这个:
using (ZMessage message = subscriber.ReceiveMessage()){
Console.WriteLine("Message received!");
string pubID = message[0].ReadString();
/*receive bytearray from publisher*/
content = message[1].Read();
Console.WriteLine("size of content: " + message[1].Length);
/*create BitmapSource out of the bytearray you receive.*/
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
BitmapSource source = BitmapSource.Create(1920, 1080, 72, 72,
PixelFormats.Bgra32, BitmapPalettes.Gray256, content, 1920 * 4);
if (source != null){
Console.WriteLine("source is created");
}
/*display image on screen*/
videoView.Source = source;
Console.WriteLine("videoView updated");
}));
}