This is for a .NET 4.5 Console Application running on a 64-bit Windows 8 system with 32GB of memory. The application is targeted for 64-bit platforms and has the gcAllowVeryLargeObjects flag enabled, which allows the application create objects larger than .NET limit of 2Gb. So, I am able to create an array of integers as large as 15GB.
I create a 32,767 by 32,767 pixel System.Windows.Media.Imaging.WriteableBitmap which uses WIC (Windows Imaging Component and not GDI+ as WIC can handle higher resolution than GDI+) using the following constructor
WriteableBitmap wbit = new WriteableBitmap(32767, 32767, 300, 300, PixelFormats.Bgra32, null);
I have tried to save it as a JPG using following code
using (FileStream stream = new FileStream("c:\\test.jpg",FileMode.Create))
{
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(wbit));
encoder.Save(stream);
}
I have also tried to save it as a PNG using following code
using (FileStream stream = new FileStream("c:\\test.png",FileMode.Create))
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(wbit));
encoder.Save(stream);
}
For both JPG and PNG, I can go up to 23,170 x 23,170 resolution, and it work fine, but going up to 23,171 x 23,171 throws the following exception
System.AccessViolationException: Attempted to read or write protected memory. Tis is often an indication that other memoryis corrupt. at MS.Wind32.PresentationCore.UnsafeNativeMethods.WICBitmapFrameEncode.WriteSource(SafeMILHandle THIS_PTR, SafeMILHandle pIBitmapSource, Int32Rect& r) at System.Windows.Media.Imaging.BitmapEncoder.Save(Stream stream)
Does anyone know the cause of this exception, and how I might be able to get around it so that I can save the full maximum resolution at 32,767 x 32,767? Maybe I have to use another encoder or even start looking outside .NET framework. I would ideally like to be able to work with images at the JPEG maximum resolution of 65,535 x 65,535, or even higher at 100,000 x 100,000 pixels in PNG. I would appreciate any help with this matter or suggestion towards the right direction. Thanks.