C# WPF,经常刷新图片而不闪烁
我正在尝试使用WPF从我的网络摄像头创建一个实时视图,它只提供HTTP上的JPG快照。 每次我更新图像源时,它都会闪烁。 有人知道如何解决这个问题吗 XAML:C# WPF,经常刷新图片而不闪烁,c#,wpf,xaml,bitmapimage,C#,Wpf,Xaml,Bitmapimage,我正在尝试使用WPF从我的网络摄像头创建一个实时视图,它只提供HTTP上的JPG快照。 每次我更新图像源时,它都会闪烁。 有人知道如何解决这个问题吗 XAML: 闪烁是因为BitmapImage是异步加载的,即在将其指定给图像的源属性之后。Image元素首先变为空,只有在下载完成后才会显示图像 在这种情况下,您可以在DownloadCompleted事件处理程序中设置Source属性: void timer_Tick(object sender, EventArgs e) { var u
闪烁是因为BitmapImage是异步加载的,即在将其指定给图像的源属性之后。Image元素首先变为空,只有在下载完成后才会显示图像 在这种情况下,您可以在DownloadCompleted事件处理程序中设置Source属性:
void timer_Tick(object sender, EventArgs e)
{
var uri = "http://0.0.0.0/api/camera/snapshot?width=640&height=480";
var image = new BitmapImage();
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
image.UriSource = new Uri(uri);
image.EndInit();
if (image.IsDownloading)
{
image.DownloadCompleted += (s, ev) => img.Source = image;
}
else
{
img.Source = image;
}
}
闪烁是因为BitmapImage是异步加载的,即在将其指定给图像的源属性之后。Image元素首先变为空,只有在下载完成后才会显示图像 在这种情况下,您可以在DownloadCompleted事件处理程序中设置Source属性:
void timer_Tick(object sender, EventArgs e)
{
var uri = "http://0.0.0.0/api/camera/snapshot?width=640&height=480";
var image = new BitmapImage();
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
image.UriSource = new Uri(uri);
image.EndInit();
if (image.IsDownloading)
{
image.DownloadCompleted += (s, ev) => img.Source = image;
}
else
{
img.Source = image;
}
}
谢谢大家的帮助。
我从你那里得到了答案
转换为异步HttpClient API而不是WebClient,它看起来像:
private readonly HttpClient httpClient = new HttpClient();
private async void timer_Tick(object sender, EventArgs e)
{
var uri = "http://0.0.0.0/api/camera/snapshot?width=640&height=480";
using (var responseStream = await httpClient.GetStreamAsync(uri))
using (var memoryStream = new MemoryStream())
{
await responseStream.CopyToAsync(memoryStream); // ensure full download
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = memoryStream;
image.EndInit();
img.Source = image;
}
}
谢谢大家的帮助。
我从你那里得到了答案
转换为异步HttpClient API而不是WebClient,它看起来像:
private readonly HttpClient httpClient = new HttpClient();
private async void timer_Tick(object sender, EventArgs e)
{
var uri = "http://0.0.0.0/api/camera/snapshot?width=640&height=480";
using (var responseStream = await httpClient.GetStreamAsync(uri))
using (var memoryStream = new MemoryStream())
{
await responseStream.CopyToAsync(memoryStream); // ensure full download
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = memoryStream;
image.EndInit();
img.Source = image;
}
}
降低分辨率,这也将减少处理。同时将间隔从1秒减少到1/30秒。如果时间快于~1/30秒,您的眼睛将看不到闪烁。这就是电影帧每秒60张图片的原因。闪烁是因为BitmapImage是异步加载的,即在您将其分配给图像的源属性之后。图像元素首先变为空,只有在下载完成后才会显示图像。是的,每次更新,背景色都会显示几毫秒,这会导致闪烁。但是我不知道如何解决这个问题。降低分辨率也会减少处理。同时将间隔从1秒减少到1/30秒。如果时间快于~1/30秒,您的眼睛将看不到闪烁。这就是电影帧每秒60张图片的原因。闪烁是因为BitmapImage是异步加载的,即在您将其分配给图像的源属性之后。图像元素首先变为空,只有在下载完成后才会显示图像。是的,每次更新,背景色都会显示几毫秒,这会导致闪烁。但我不知道如何解决这个问题。