.NET GDI + File Size - File Codec Limit

Is there a limit on the size of an image that can be encoded using image file codecs available from .NET?

I'm trying to encode images> 4 GB in size, but it just doesn't work (or doesn't work properly, that is, it writes an unreadable file) using .bmp, .jpg, .png or .tif encoders.

When I reduce the image size to <2GB it works with .jpg, but not with .bmp, .tif or .png.

My next attempt is to try libtiff because I know that tiff files are for large images.

What is a good file format for large images? or am I just pushing file format restrictions?

(All this runs on a 64-bit operating system (WinXP 64) with 8 GB of RAM and compiled using the x64 architecture.)

Random r = new Random((int)DateTime.Now.Ticks); int width = 64000; int height = 64000; int stride = (width % 4) > 0 ? width + (width % 4) : width; UIntPtr dataSize = new UIntPtr((ulong)stride * (ulong)height); IntPtr p = Program.VirtualAlloc(IntPtr.Zero, dataSize, Program.AllocationType.COMMIT | Program.AllocationType.RESERVE, Program.MemoryProtection.READWRITE); Bitmap bmp = new Bitmap(width, height, stride, PixelFormat.Format8bppIndexed, p); BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); ColorPalette cp = bmp.Palette; for (int i = 0; i < cp.Entries.Length; i++) { cp.Entries[i] = Color.FromArgb(i, i, i); } bmp.Palette = cp; unsafe { for (int y = 0; y < bd.Height; y++) { byte* row = (byte*)bd.Scan0.ToPointer() + (y * bd.Stride); for (int x = 0; x < bd.Width; x++) { *(row + x) = (byte)r.Next(256); } } } bmp.UnlockBits(bd); bmp.Save(@"c:\test.jpg", ImageFormat.Jpeg); bmp.Dispose(); Program.VirtualFree(p, UIntPtr.Zero, 0x8000); 

I also tried using the GC pinned memory area, but this is limited to <2 GB.

 Random r = new Random((int)DateTime.Now.Ticks); int bytesPerPixel = 4; int width = 4000; int height = 4000; int padding = 4 - ((width * bytesPerPixel) % 4); padding = (padding == 4 ? 0 : padding); int stride = (width * bytesPerPixel) + padding; UInt32[] pixels = new UInt32[width * height]; GCHandle gchPixels = GCHandle.Alloc(pixels, GCHandleType.Pinned); using (Bitmap bmp = new Bitmap(width, height, stride, PixelFormat.Format32bppPArgb, gchPixels.AddrOfPinnedObject())) { for (int y = 0; y < height; y++) { int row = (y * width); for (int x = 0; x < width; x++) { pixels[row + x] = (uint)r.Next(); } } bmp.Save(@"c:\test.jpg", ImageFormat.Jpeg); } gchPixels.Free(); 
+6
gdi + codec
source share
3 answers

The problem you are facing is very likely the following restriction in the .NET Framework: the maximum object size allowed in GC Heap is 2 GB, even in 64-bit OS / assemblies.

Some links: link , link , link .

Depending on your needs, with a relatively simple (uncompressed) format, such as TIFF, it can be done to create your image in parts and then combine the parts. Just an idea ..

+2
source share

From the TIFF Specification Section 2: TIFF Structure at http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf :

TIFF is an image file format. In this document, a file is defined as a sequence of 8-bit bytes, where the bytes are numbered from 0 to N. The maximum possible TIFF file is 2 ** 32 bytes in length.

Unfortunately, many TIFF authors and readers use signed integers in the implementation and reduce the practical size to 2 ** 31. This is compounded by the frequent attempts by drivers to store the entire image in memory, as mentioned by Christoph.

You will probably find similar limitations in other image formats created in the 80s or earlier. Restrictions on file sizes of several gigabytes were not considered a problem when disks were only in tens of megabytes.

+1
source share

So your goal is to code them in jpeg? I am sure that it will be possible to use only the section that you are coding, then dispose of and proceed to the next section. If you open the raw file and parse it yourself, I'm sure there are open source jpeg encoders to help create partitions. This should allow you to encode files extremely large, but you will always have problems with the system and file system.

+1
source share

All Articles