Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use large bitmaps in .NET?
    text
    copied!<p>I'm trying to write a light-weight image viewing application. However, there are system memory limitations with .NET.</p> <p>When trying to load large bitmaps (<strong>9000 x 9000 px</strong> or larger, 24-bit), I get a System.OutOfMemoryException. This is on a Windows 2000 PC with 2GB of RAM (of which 1.3GB is used up). It also takes a lot of time to attempt loading the files.</p> <p>The following code generates this error:</p> <pre><code>Image image = new Bitmap(filename); using (Graphics gfx = this.CreateGraphics()) { gfx.DrawImage(image, new Point(0, 0)); } </code></pre> <p>As does this code:</p> <pre><code>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); } </code></pre> <p>Also, it is enough to do just this:</p> <pre><code>Bitmap bitmap = new Bitmap(filename); IntPtr handle = bitmap.GetHbitmap(); </code></pre> <p>The latter code was intended for use with GDI. While researching this, I found out that this is in fact a memory issue where .NET tries to allocate twice as much as is needed in a single contigous block of memory.</p> <p><a href="http://bytes.com/groups/net-c/279493-drawing-large-bitmaps" rel="noreferrer">http://bytes.com/groups/net-c/279493-drawing-large-bitmaps</a></p> <p>I know from other applications (Internet Explorer, MS Paint etc.) that it IS possible to open large images, and rather quickly. My question is, <strong><em>how do I use large bitmaps with .NET?</em></strong></p> <p>Is there anyway to stream them, or non-memory load them?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload