异步加载大型BitmapImage

jms*_*era 2 .net wpf async-await

我正在尝试将非常大的图像(大约16000x7000像素)加载到材质中,并且我尝试异步加载它.我的第一个尝试是创建一个加载BitmapImage的任务,然后将其用于材质:

var bmp = await System.Threading.Tasks.Task.Run(() =>
{
    BitmapImage img = new BitmapImage(new Uri(path));
    ImageBrush brush = new ImageBrush(img);
    var material = new System.Windows.Media.Media3D.DiffuseMaterial(brush);
    material.Freeze();
    return material;
});
BackMaterial = bmp;
Run Code Online (Sandbox Code Playgroud)

但是我发现的是,在显示材质之前,图像没有被加载并扩展到内存中(如果我直接使用ImageBrush则相同).

我试图避免这种情况,因为它冻结了我的UI,但我没有找到强制位图加载和解码的正确方法.如果我添加一个WriteableBitmap,则在Task内部执行图片的加载,但之后我将使用的内存量增加一倍:

var bmp = await System.Threading.Tasks.Task.Run(() =>
{
    BitmapImage img = new BitmapImage(new Uri(path));
    WriteableBitmap wbmp = new WriteableBitmap(img);
    ImageBrush brush = new ImageBrush(wbmp);
    var material = new System.Windows.Media.Media3D.DiffuseMaterial(brush);
    material.Freeze();
    return material;
});
BackMaterial = bmp;
Run Code Online (Sandbox Code Playgroud)

有没有办法强制加载而不会将其加倍到内存中.我也尝试用解码器加载它,但我也加载到内存中两次:

var decoder = BitmapDecoder.Create(new Uri(path), BitmapCreateOptions.PreservePixelFormat,
     BitmapCacheOption.None);
var frame = decoder.Frames[0];
int stride = frame.PixelWidth * frame.Format.BitsPerPixel / 8;
byte[] lines = new byte[frame.PixelHeight * stride];
frame.CopyPixels(lines, stride, 0);
var img = BitmapImage.Create(
    frame.PixelWidth, frame.PixelHeight, frame.DpiX, frame.DpiY, frame.Format,
    frame.Palette, lines, stride);
frame = null;
lines = null;
Run Code Online (Sandbox Code Playgroud)

谢谢!

Cle*_*ens 5

我不确定这种异步情况,但你通常会设置BitmapCacheOption.OnLoad来强制立即缓存到内存:

var bmp = await System.Threading.Tasks.Task.Run(() => 
{ 
    BitmapImage img = new BitmapImage(); 
    img.BeginInit(); 
    img.CacheOption = BitmapCacheOption.OnLoad; 
    img.UriSource = new Uri(path); 
    img.EndInit(); 
    ImageBrush brush = new ImageBrush(img); 
    ...
}
Run Code Online (Sandbox Code Playgroud)