I'm trying to load a very large image (about 16000x7000 pixel) into a Material, and I've tried to load it asynchronously. My first try was to create a task that loaded the BitmapImage and then use it into the material:
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;
But what I've found is that the image it is not loaded and expanded into memory until the material is shown (it's the same if I use the ImageBrush directly).
I'm trying to avoid that because it freezes my UI, but I haven't found the right way to force the bitmap loadi and decode. If I add a WriteableBitmap the load of the picture is performed inside the Task, but then I'm doubling the amount of memory being used:
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;
Is there any way to force the load without doubling it into memory. I also tried to load it with a decoder, but I'm also loading into memory twice:
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;
Thanks!
See Question&Answers more detail:os