title: (C++/WinRT) How can I preserve Bitmap transparency when scaling?
body: <p>I'm manually scaling a GIF so that I can set the InterpolationMode on it. However, while the GIF is appropriately transparent at its original size, and when scaled in the XAML, BitmapTransform().ScaledWidth() and BitmapTransform().ScaledHeight() seem to cause the frame to lose its transparency.</p>
  <p>How can I maintain transparency while still interpolating manually?</p>
  <pre><code>&lt;!-- XamlFile --&gt;
  &lt;Image x:Name=&quot;Sprite&quot; Stretch=&quot;None&quot; Width=&quot;300&quot; Height=&quot;300&quot; /&gt;
  </code></pre>
  <pre><code>// C++ File
  IAsyncAction XamlFile::ScaleSpriteAsync(hstring path) {
      // get the file from a URL
      auto streamRef{ RandomAccessStreamReference::CreateFromUri(Uri{path}) };
      auto stream{co_await streamRef.OpenReadAsync() };
      auto decoder{ co_await BitmapDecoder::CreateAsync(stream) };
      auto ras{ InMemoryRandomAccessStream() };
      auto encoder{ co_await BitmapEncoder::CreateForTranscodingAsync(ras, decoder) };
      for (auto frame = 0; frame &lt; decoder.FrameCount(); frame++) {
          // transform each frame of the GIF
          encoder.BitmapTransform().InterpolationMode(BitmapInterpolationMode::NearestNeighbor);
          encoder.BitmapTransform().ScaledWidth(200);
          encoder.BitmapTransform().ScaledHeight(200);
          // commit the frame unless it's the last frame
          if (frame != (decoder.FrameCount() - 1)) {
              co_await encoder.GoToNextFrameAsync();
          }
      }
      try {
          co_await encoder.FlushAsync();
      }
      catch (hresult_error const&amp; ex) {
          // ...
      }
      auto img{ BitmapImage() };
      img.SetSource(ras);
      Sprite().Source(img);
  }
  </code></pre>
  <p>Note that this also occurs for other image types such as PNGs.</p>
  <p><a href="https://i.stack.imgur.com/ZyY78.gif" rel="nofollow noreferrer">Demo GIF (first 4 frames are scaled)</a></p>