我正在尝试编写一个轻量级的图像查看应用程序。但是,.NET 存在系统内存限制。
尝试加载大型位图(9000 x 9000 像素或更大,24 位)时,我收到 System.OutOfMemoryException。这是在具有 2GB RAM(其中 1.3GB 已用完)的 Windows 2000 PC 上。尝试加载文件也需要很多时间。
以下代码生成此错误:
Image image = new Bitmap(filename);
using (Graphics gfx = this.CreateGraphics())
{
gfx.DrawImage(image, new Point(0, 0));
}
与此代码一样:
Stream stream = (Stream)File.OpenRead(filename);
Image image = Image.FromStream(stream, false, false);
using (Graphics gfx = this.CreateGraphics())
{
gfx.DrawImage(image, new Rectangle(0, 0, 100, 100), 4000, 4000, 100, 100, GraphicsUnit.Pixel);
}
此外,这样做就足够了:
Bitmap bitmap = new Bitmap(filename);
IntPtr handle = bitmap.GetHbitmap();
后一个代码旨在与 GDI 一起使用。在研究这一点时,我发现这实际上是一个内存问题,其中 .NET 尝试在单个连续内存块中分配两倍于所需的内存。
http://bytes.com/groups/net-c/279493-drawing-large-bitmaps
我从其他应用程序(Internet Explorer、MS Paint 等)中知道可以打开大图像,而且速度相当快。我的问题是,如何在 .NET 中使用大型位图?
无论如何要流式传输它们,还是非内存加载它们?