在WPF中显示时使用过多内存的C#位图

时间:2016-01-22 16:37:34

标签: c# wpf bitmap bitmapimage

我有一个类,它允许我在位图上绘制线条以创建粗略的地图。当我创建一个大位图时,它会使用我期望的内存量(10,000x10,000图像使用400MB)。

要在WPF窗口中显示图像,我有以下代码:

imageBox.Source = mapBuilder.getMapInDisplayFormat();

哪个电话

public BitmapImage getMapInDisplayFormat()
{
    //areaBitMap is the underlying bitmap which has been drawn
    areaBitMap.RotateFlip(RotateFlipType.RotateNoneFlipY); //Flip image in the Y axis so 0,0 is in the bottom left corner like a graph (not top left)
    BitmapImage bitmapImage = new BitmapImage();
    using (MemoryStream memory = new MemoryStream())
    {
        areaBitMap.Save(memory, ImageFormat.Bmp);
        areaBitMap.RotateFlip(RotateFlipType.RotateNoneFlipY); //After saving to the mem stream flip the image back so future drawings are to the correct location
        memory.Position = 0;                
        bitmapImage.BeginInit();
        bitmapImage.StreamSource = memory;
        bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
        bitmapImage.EndInit();
        memory.Dispose();
    }
    return bitmapImage;
}

显示图像后,程序内存使用量增加到1.2GB(存储位图所需内存的3倍)。我可以理解BitmapImage有一个单独的内存分配需要800MB(总计),但我不知道为什么内存使用量是我预期的3倍。 (注意:400MB就是一个例子,这种模式适用于任何大小的位图,但它只是一个较大图像的问题)。任何人都可以解释为什么内存使用率如此之高?

我从代码中将代码转换为BitmapImage。这是在屏幕上显示图像的最佳方式吗?这看起来效率很低。您可以原生地显示没有转换的位图吗?

0 个答案:

没有答案