每秒闪烁更新一次 BitmapImage

2023-12-20

我试图通过每秒设置源属性来更新图像,这有效,但更新时会导致闪烁。

CurrentAlbumArt = new BitmapImage();
CurrentAlbumArt.BeginInit();
CurrentAlbumArt.UriSource = new Uri((currentDevice as AUDIO).AlbumArt);
CurrentAlbumArt.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
CurrentAlbumArt.EndInit();

如果我不设置IgnoreImageCache,图像不会更新,因此也不会闪烁。

有办法绕过这个警告吗?

Cheers.


以下代码片段下载整个图像缓冲区before设置图像的Source属性到一个新的 BitmapImage。这应该可以消除任何闪烁。

var webClient = new WebClient();
var url = ((currentDevice as AUDIO).AlbumArt;
var bitmap = new BitmapImage();

using (var stream = new MemoryStream(webClient.DownloadData(url)))
{
    bitmap.BeginInit();
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.StreamSource = stream;
    bitmap.EndInit();
}

image.Source = bitmap;

如果下载需要一些时间,那么在单独的线程中运行它是有意义的。然后,您还必须通过调用来注意正确的跨线程访问Freeze在 BitmapImage 上并分配Source在调度程序中。

var bitmap = new BitmapImage();

using (var stream = new MemoryStream(webClient.DownloadData(url)))
{
    bitmap.BeginInit();
    bitmap.CacheOption = BitmapCacheOption.OnLoad;
    bitmap.StreamSource = stream;
    bitmap.EndInit();
}

bitmap.Freeze();
image.Dispatcher.Invoke((Action)(() => image.Source = bitmap));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

每秒闪烁更新一次 BitmapImage 的相关文章

随机推荐