(C++/WinRT) 缩放时如何保持位图透明度?

问题描述

我正在手动缩放 GIF,以便可以在其上设置 InterpolationMode。然而,虽然 GIF 在其原始大小下适当透明,并且在 XAML 中缩放时,BitmapTransform().ScaledWidth() 和 BitmapTransform().ScaledHeight() 似乎导致框架失去其透明度。

如何在手动插值的同时保持透明度?

<!-- XamlFile -->
<Image x:Name="Sprite" Stretch="None" Width="300" Height="300" />
// 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 < 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& ex) {
        // ...
    }
    auto img{ BitmapImage() };
    img.SetSource(ras);
    Sprite().source(img);
}

请注意,其他图像类型(例如 PNG)也会出现这种情况。

Demo GIF (first 4 frames are scaled)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)