0

I need to create a bitmap where the size of the image can go beyond 2GB. I use:

var myPalette = new BitmapPalette(colors);
var buffer = new byte[image.ImageHeight * image.ImageWidth];

The above lines throws an exception Array dimensions exceeded supported range, when image.ImageHeight * image.ImageWidth crosses 2GB as expected. But is there any solution so that I can still get a bitmap image of expected size without losing any data? Currently, I am generating bitmap as shown below.

var colors = new List<Color> { Colors.Black, Colors.Yellow, Colors.Blue, Colors.Red, Colors.LimeGreen,Colors.Aqua};
var bitMapImage = BitmapSource.Create(width, height, 96, 96, PixelFormats.Indexed8, myPalette, buffer, width);
Andre Hofmeister
  • 3,185
  • 11
  • 51
  • 74
  • 1
    https://stackoverflow.com/questions/30895549/cant-create-huge-arrays – Marco Nov 26 '18 at 13:59
  • Just a side note: if you need to operate on 2GB+ bitmaps, there's probably something wrong with your code. – dymanoid Nov 26 '18 at 14:10
  • Possible duplicate of [Can't create huge arrays](https://stackoverflow.com/questions/30895549/cant-create-huge-arrays) – Peter B Nov 26 '18 at 14:11
  • Read some thoughts on a similar problem at https://blogs.msdn.microsoft.com/joshwil/2005/08/10/bigarrayt-getting-around-the-2gb-array-size-limit/ . Now I don't think you can use `Marshal.AllocHGlobal` because it takes an `Int32` but I *think* you can pinvoke `localalloc` directly because it takes a `size_t`: https://learn.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-localalloc – BurnsBA Nov 26 '18 at 14:17
  • However, you may run into problems with your app not trusting the memory because it's no longer managed, so you'll run into other problems that need to be addressed. https://blog.getpaint.net/2012/04/30/marshaling-native-arrays-back-as-managed-arrays-without-copying/ – BurnsBA Nov 26 '18 at 14:20
  • I understand that there is no way to create the array. but is there any way where I can get still get the bitmap using any other constructor of bitmapsource – Developer123 Nov 26 '18 at 14:21
  • Read following posting : https://stackoverflow.com/questions/9888192/creating-huge-high-resolution-bitmap-bigger-than-23k-x-23k – jdweng Nov 26 '18 at 14:26
  • Can i have a condition to check new byte[image.ImageHeight * image.ImageWidth]; will fail before creating it. I tried checking image.ImageHeight * image.ImageWidth <=2e+9 but that is not helping – Developer123 Nov 29 '18 at 05:10

0 Answers0