从 MemoryStream 乘以图像源

Multiply Image Source From MemoryStream

我使用以下代码将图像文件转换为 96 DPI 并将其用作背景。

BitmapSource bitmapSource = ConvertBitmapTo96Dpi(CompleteBackgroundImage);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
MemoryStream memoryStream = new MemoryStream();

encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
encoder.Save(memoryStream);

memoryStream.Position = 0;

CompleteBackgroundImage = new BitmapImage();
CompleteBackgroundImage.BeginInit();
CompleteBackgroundImage.StreamSource = memoryStream;
CompleteBackgroundImage.CacheOption = BitmapCacheOption.OnLoad;
CompleteBackgroundImage.DecodePixelHeight = (int)Math.Round(finalHeight, MidpointRounding.AwayFromZero);
CompleteBackgroundImage.DecodePixelWidth = (int)Math.Round(finalWidth, MidpointRounding.AwayFromZero);
CompleteBackgroundImage.EndInit();

memoryStream.Close();

现在我想,如果屏幕比文件大,将图像乘以一个图像,这样我就可以将它用作背景。 因此,如果我的屏幕是 1920 x 1080 而图像只有 500 x 500,我希望该图像与 2000 x 1500 一样大并且原始图像必须从左到右显示 4 次,从左到右显示 3 次从上到下。

但是我应该怎么做呢?

考虑使用 TileMode 设置为 Tile 的 ImageBrush:

<Window.Background>
    <ImageBrush TileMode="Tile" ViewportUnits="Absolute" Viewport="0,0,512,384">
        <ImageBrush.ImageSource>
            <BitmapImage UriSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"/>
        </ImageBrush.ImageSource>
    </ImageBrush>
</Window.Background>

或更短:

<Window.Background>
    <ImageBrush TileMode="Tile" ViewportUnits="Absolute" Viewport="0,0,512,384"
                ImageSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"/>
</Window.Background>