dlemstra / Magick.NET

The .NET library for ImageMagick

Repository from Github https://github.comdlemstra/Magick.NETRepository from Github https://github.comdlemstra/Magick.NET

How to convert huge image to Bmp format ?

Charltsing opened this issue · comments

Magick.NET version

13.8.0

Environment (Operating system, version and so on)

Windows10 64bit, netframework 4.7.2,
x64, 8G memory
Magick.NET-Q16-AnyCPU.dll, Magick.Native-Q16-x64.dll, 13.8.0
ImageMagick.ResourceLimits.LimitMemory = 85%

I have a huge jpg file w26493x h37200, 279M 37200.JPG

MemoryStream outstream = new MemoryStream();
byte[] picbyte = File.ReadAllBytes("37200.jpg");
using (MagickImage magickimage = new MagickImage())
{
      magickimage.Read(picbyte);
      magickimage.Write(outstream, MagickFormat.Bmp);  
      // outstream.Length = 2,147,475,594 , 
      // Perhaps the length of outstream is incorrect. 26493 x 37200 x 3 = 2,956,618,800。
      // Is there a 2G limit for out stream ?

      Bitmap bmp = new Bitmap(outstream);   // it report invalid parameter error
}

11

2

I try add gcAllowVeryLargeObjects in App.Config, but it didn't work

try Bitmap.FromFile("37200.jpg");
it report error : System.OverflowException.


then I try net8, x64, Magick.Native-Q16-HDRI-OpenMP-x64.dll, 13.9.1

MagickImage imageM = new ();
byte[] raw = File.ReadAllBytes("37200.jpg");
imageM.Read(raw); 

it report error :
ImageMagick.MagickCacheErrorException:“unable to extend cache '': Invalid argument @ error/cache.c/OpenPixelCache/3925”
31

Did I do something wrong ? Is this error related to memory size ?

You are running into the memory limits of a MemoryStream. The length of a MemoryStream can not be larger than int.MaxValue.

so, How to convert huge image to Bmp format? If it is not possible to convert a image to a bitmap larger than 2G in
memory, is there any other way to modify the huge image pixel data in memory?

Can only cut the image and then piece it together ?

You are running into limitations of .NET. There is not much I can do about that. There might be custom implementations of MemoryStream that support more than 2GB but you will need to find that yourself.