大佬,B站粉丝过来提问交流的。是关于WPF中加载大量本地or远程大型图片文件(超过20MB)时,UI线程会被阻塞导致窗口卡顿的问题。 #231
Replies: 7 comments 10 replies
-
|
本地文件夹是不是有很多图片?你可以搜索WPF虚拟化技术 |
Beta Was this translation helpful? Give feedback.
-
|
你给 |
Beta Was this translation helpful? Give feedback.
-
|
哥们开源了吗,开源了的话po一下链接吧,可以向你学习学习 |
Beta Was this translation helpful? Give feedback.
-
|
你好 你已经修改好了么 我是新手wpf小白 我想看看你改好的样子可以不 方便我以后在我的项目里也能有更好的体验 方便给个代码片段或者给个你开源的那个具体代码地址 |
Beta Was this translation helpful? Give feedback.
-
|
建议使用writebitmap,通过视口操作,比如界面只展示十张照片,滑动的时候加载新的十张到界面上来。writebitmap直接通过指针操作很快的 |
Beta Was this translation helpful? Give feedback.
-
|
还有一个技巧,你可以在子线程加载好图片,然后 Freeze 它,给主线程用。这样,加载的逻辑不在主线程,自然也不会有什么卡主线程的问题。 |
Beta Was this translation helpful? Give feedback.
-
|
如果正确启用了 UI 虚拟化,可以尝试异步绑定图片, public static class ImageHelper
{
public static BitmapImage? ImageToBitmapImage(Image? img)
{
if (img == null)
{
return null;
}
using var memory = new MemoryStream();
img.Save(memory, ImageFormat.Png);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
return bitmapImage;
}
} |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
自学了小半年的WPF后,最近在搞一个Windows桌面应用,准备开源。但是这个问题一直困扰着我,始终没能很好的解决,使用Dispatcher.BeginInvoke异步加载也不行,没脸皮开源😅,请大佬赐教!😁
以下是新建工程的实例代码,一个列表加载本地文件夹中的图片,均超过20MB大小,在图片没有加载显示出来的时候拖动窗口是拖不动的or卡顿的。
MainWindow.xaml
MainWindow.xaml.cs
Beta Was this translation helpful? Give feedback.
All reactions