<!-- Simple image rendering. However, rendering an image this way may not result in the best use of application memory. See markup below which creates the same end result but using less memory. --> <Image Width="200" Source="Sample.jpg"/>其中,Sample.jpg 是一张分辨率为 7360 x 4912 的图片。启动后的程序内存占用 534 MB。
<Image Width="200"> <Image.Source> <!-- To save significant application memory, set the DecodePixelWidth or DecodePixelHeight of the BitmapImage value of the image source to the desired height and width of the rendered image. If you don't do this, the application will cache the image as though it were rendered as its normal size rather than just the size that is displayed. --> <!-- Note: In order to preserve aspect ratio, only set either DecodePixelWidth or DecodePixelHeight but not both. --> <BitmapImage DecodePixelWidth="200" UriSource="Sample.jpg" /> </Image.Source> </Image>
内存占用降低为 65 MB,效果非常明显。