Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to resize bitmap image to be <200 KB and meet Tile restrictions (WinRT)
    primarykey
    data
    text
    <p>I am developing a routine to scale some bitmap images to be part of tile notifications for my Window-8 app</p> <p>The tile images must be &lt;200KB and less than 1024x1024 px in dimension. I am able to use a scaling routine to resize the source image as necessary to fit the 1024x1024 px dimension limitation.</p> <p>How can I alter the source image to guarantee the size restriction will be met?</p> <p>My first attempt was to continue to scale down the image until it clears the size threshold, and use <code>isTooBig = destFileStream.Size &gt; MaxBytes</code> to determine the size. But, the code below results in an infinite loop. How can I reliably measure the size of the destination file?</p> <pre><code> bool isTooBig = true; int count = 0; while (isTooBig) { // create a stream from the file and decode the image using (var sourceFileStream = await sourceFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) using (var destFileStream = await destFile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(sourceFileStream); BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(destFileStream, decoder); double h = decoder.OrientedPixelHeight; double w = decoder.OrientedPixelWidth; if (h &gt; baselinesize || w &gt; baselinesize) { uint scaledHeight, scaledWidth; if (h &gt;= w) { scaledHeight = (uint)baselinesize; scaledWidth = (uint)((double)baselinesize * (w / h)); } else { scaledWidth = (uint)baselinesize; scaledHeight = (uint)((double)baselinesize * (h / w)); } //Scale the bitmap to fit enc.BitmapTransform.ScaledHeight = scaledHeight; enc.BitmapTransform.ScaledWidth = scaledWidth; } // write out to the stream await enc.FlushAsync(); await destFileStream.FlushAsync(); isTooBig = destFileStream.Size &gt; MaxBytes; baselinesize *= .90d * ((double)MaxBytes / (double)destFileStream.Size); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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