Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad a large BitmapImage asynchronously
    primarykey
    data
    text
    <p>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:</p> <pre><code>var bmp = await System.Threading.Tasks.Task.Run(() =&gt; { 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; </code></pre> <p>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).</p> <p>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:</p> <pre><code>var bmp = await System.Threading.Tasks.Task.Run(() =&gt; { 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; </code></pre> <p>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:</p> <pre><code>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; </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload