微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决(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 举报,一经查实,本站将立刻删除。