Create a valid image object from generated BMP File

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
pablobhz
Posts: 76
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Create a valid image object from generated BMP File

Post by pablobhz »

Hello Everyone.
I'm dealing with a strange problem:
I'm receiving an bmp file created trough an Android App - my final goal is to print it
Before trying to create a MagickImage object, i do run trough all its bytes and adjust them. On the Android App the Bitmap is created using Bitmap.Config.ARGB_8888, and according to the documentation it stores one pixel per 4 bytes.

So far so good, i got an routine to create an bgra image.

However when i try to read it on ImageMagick, it throws the exception:
"System.ArgumentException: 'The array count is 2858400 but should be at least 2880000.
Parameter name: data'"

If i try to create the image from the original file (without rearranging its bytes), i am receiving the same exception.

If i try to use LockBits to retrieve the image data as BMP i am receiving an System Violation Exception.

Is there any way i can use MagickImage to get this image valid ?
I uploaded an sample image to this google drive link.
https://drive.google.com/file/d/1hpnLp4 ... sp=sharing

Thanks in advance

Pablo

Code:

Code: Select all

var imageData = File.ReadAllBytes(@"C:\Users\Pablo\Desktop\Image-4158.bmp");
         
            var not255 = imageData.Where(b => b != 255).ToList();
            byte[] newData = new byte[imageData.Length];

            for (int x = 0; x < imageData.Length; x += 4)
            {
                byte[] pixel = new byte[4];
                Array.Copy(imageData, x, pixel, 0, 4);

                byte r = pixel[0];
                byte g = pixel[1];
                byte b = pixel[2];
                byte a = pixel[3];

                byte[] newPixel = new byte[] { r, g, b, a };

                Array.Copy(newPixel, 0, newData, x, 4);
            }
           imageData = newData;
            PixelReadSettings set = new PixelReadSettings(600, 1200, StorageType.Char, PixelMapping.RGBA);
            MagickImage finalImg = new MagickImage(imageData, set);
            

snibgo
Posts: 12682
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Create a valid image object from generated BMP File

Post by snibgo »

You seem to think your input file has 600x1200 pixels. If each pixel takes 4 bytes, this would be 2880000 bytes. But your input Image-4158.bmp has fewer bytes.

Incidentally, Image-4158.bmp is not a valid BMP file, so it is unwise to use a ".bmp" extension.
snibgo's IM pages: im.snibgo.com

pablobhz
Posts: 76
Joined: 2016-05-07T06:47:14-07:00
Authentication code: 1151

Re: Create a valid image object from generated BMP File

Post by pablobhz »

snibgo wrote:
2019-10-29T06:39:01-07:00
You seem to think your input file has 600x1200 pixels. If each pixel takes 4 bytes, this would be 2880000 bytes. But your input Image-4158.bmp has fewer bytes.

Incidentally, Image-4158.bmp is not a valid BMP file, so it is unwise to use a ".bmp" extension.
When using the same code to generate a BMP with 400x1200, it works. The point is that i'm trying to achieve an working result if possible.

This file can't have an image created from it ? SOmething with the byte order maybe ?

snibgo
Posts: 12682
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Create a valid image object from generated BMP File

Post by snibgo »

pablohz wrote:This file can't have an image created from it ?
It has only 2858400 bytes. At 4 bytes/pixel, perhaps it contains 600x1191 pixels.
snibgo's IM pages: im.snibgo.com

Post Reply