CacheData

RasterCachedImage.CacheData method

缓存数据并确保不会从底层执行额外的数据加载DataStreamContainer .

public override void CacheData()

例子

以下示例显示了光栅图像缓存如何影响性能。在一般情况下,读取缓存数据比读取非缓存数据执行得更快。

[C#]

string dir = "c:\\temp\\";

// 从 PNG 文件中加载图像。
using (Aspose.Imaging.RasterCachedImage image = (Aspose.Imaging.RasterCachedImage)Aspose.Imaging.Image.Load(dir + "sample.png"))
{
    // 缓存所有像素数据,这样就不会从底层数据流执行额外的数据加载
    image.CacheData();

    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();

    // 读取所有像素非常快。
    for (int y = 0; y < image.Height; y++)
    {
        for (int x = 0; x < image.Width; x++)
        {
            int color = image.GetArgb32Pixel(x, y);
        }
    }

    stopwatch.Stop();
    System.Console.WriteLine("Reading all cached pixels took {0} ms.", stopwatch.ElapsedMilliseconds);
}

// 从 PNG 文件中加载图像
using (Aspose.Imaging.RasterCachedImage image = (Aspose.Imaging.RasterCachedImage)Aspose.Imaging.Image.Load(dir + "sample.png"))
{
    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();

    // 读取所有像素不如缓存时快
    for (int y = 0; y < image.Height; y++)
    {
        for (int x = 0; x < image.Width; x++)
        {
            int color = image.GetArgb32Pixel(x, y);
        }
    }

    stopwatch.Stop();
    System.Console.WriteLine("Reading all pixels without preliminary caching took {0} ms.", stopwatch.ElapsedMilliseconds);
}

// 输出可能如下所示:
// 读取所有缓存的像素需要 1500 毫秒。
// 在没有初步缓存的情况下读取所有像素需要 150000 毫秒。

也可以看看