BitmapImage METRO重用WPF代码

本文关键字:代码 WPF 重用 METRO BitmapImage | 更新日期: 2023-09-27 18:04:04

我使用IronPython和WPF编写了一个小游戏用于教学目的,现在我想将该项目翻译为Metro APP用于测试共享项目。

有罪代码是:

def LoadImage(name, sourceRect):
    bmp = BitmapImage()
    bmp.BeginInit()
    bmp.UriSource = Uri("./data/images/" + name, UriKind.Relative)
    bmp.SourceRect = sourceRect
    bmp.EndInit()
    image = Image()
    image.Source = bmp
    return image

如何在地球上我可以获得相同的结果在地铁应用程序(使用c#)?必须有一种像旧BitmapImage那样简单的方法来做到这一点。我需要这个,因为我有平铺图像,我想要它的一部分显示。WriteableBitmap的工作,但忽略了图像的透明度,所以它是无用的。

BitmapImage METRO重用WPF代码

我一直在使用这段代码来加载缩放后的图像:

    public static async Task SetSourceAsync(
        this WriteableBitmap writeableBitmap,
        IRandomAccessStream streamSource,
        uint decodePixelWidth,
        uint decodePixelHeight)
    {
        var decoder = await BitmapDecoder.CreateAsync(streamSource);
        using (var inMemoryStream = new InMemoryRandomAccessStream())
        {
            var encoder = await BitmapEncoder.CreateForTranscodingAsync(inMemoryStream, decoder);
            encoder.BitmapTransform.ScaledWidth = decodePixelWidth;
            encoder.BitmapTransform.ScaledHeight = decodePixelHeight;
            await encoder.FlushAsync();
            inMemoryStream.Seek(0);
            await writeableBitmap.SetSourceAsync(inMemoryStream);
        }
    }

您可以使用类似的东西,但您指定encoder.BitmapTransform.Bounds而不是ScaledWidth/Height来进行裁剪。如果您有更具体的问题,请说明