Parameter is not valid exception showing on Bitmap creating when passing memory stream, only happening on 64-bit server

StackOverflow https://stackoverflow.com/questions/16483619

Pregunta

Well earlier it seemed like a similar issue asked in several forums, but in the end no solution given seems to work.

I am getting a Base64 string, which is converted to byte array. Then I am creating bitmap header and putting it into memory stream. Then trying to create Bitmap object passing stream.

The code is working successfully when I deploy the website to 32 bit server. But it is not working, in case of 64-bit server(windows server 2008 R2).

The code is as follows:

            using (MemoryStream BmpStream = new MemoryStream())
            {
                using (BinaryWriter BitmapWriter = new BinaryWriter(BmpStream))
                {
                    //m_BinaryData is the byte array
                    m_DataOffset = 54 + m_Palette.Count * 4;
                    m_DataSize = m_BinaryData.Length;
                    m_FileSize = m_DataOffset + m_DataSize;

                    //Create 54 bytes long bitmap header
                    ....

                    //Write palette
                    WriteColorTable(BitmapWriter);
                    BitmapWriter.Write(m_BinaryData);
                    BmpStream.Position = 0;                        
                    using (Bitmap TempB = new Bitmap(BmpStream))
                    {
                        m_GifStream = new MemoryStream();
                        m_GifStream.Seek(0, SeekOrigin.Begin);
                        TempB.Save(m_GifStream,    System.Drawing.Imaging.ImageFormat.Gif);
                    } 

Note: We are developing the application using 32-bit machine, Visual Studio 2010. I tried building the platform to any CPU, that too is not showing any change and the same issue is coming every time.

Please Help

Update

It's happening only with images, which have palette, with 8 bit-per-pixel depth.

Update

It is happening with only 8bpp image. Tested with 1bpp, 4bpp, 24bpp images but it's only happening with 8bpp. One more point, I tried creating color palette with all 256 colors and passed the same for 8bpp image, then no error is thrown. But the image created is all black.

Anything to do with 8bpp color palette?? That too on x64 machine.

¿Fue útil?

Solución

I have found the answer. The color palette contained alpha channel. The link

Similar issue

helped me find out the root cause of issue. The .NET Bitmap class seems to fail reading a color with an alpha channel under 64-bit. The workaround is to use an Aurigma Bitmap, which (unlike a .NET Bitmap) can handle CMYK images with or without an alpha channel.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top