0

I am trying to display a bitmap that is stored in a binary file.

The binary file is set up like so:

  1. A Header structure that is 1024 bytes.
  2. Pixels for the bitmap image (width * height * bytesperpixel - the header struct contains this info).

First I create a byte array:

var headerArray = new byte[Marshal.SizeOf(typeof(TestClass.BMPStruct))]; //size of the struct here is 1024

Then I create a FileStream on the file and read in the header first. I get bitmap's width + height + bytesperpixel from the header struct, and then read in the right amount of bytes after the header.

I then create a memory stream on those bytes and try creating a new bitmap.

using (FileStream fs = new FileStream(@"C:\mydrive\testFile.onc", FileMode.Open))
{
    fs.Position = 0; //make sure the stream is at the beginning
    fs.Read(headerArray, 0, 1024); //filestream position is 1024 after this
    var headerStruct = StructsHelper.ByteArrayToStructure<TestClass.BMPStruct>(headerArray);
    int bytesperpixel = headerStruct.BitsPerPixel / 8; //headerStruct.BitsPerPixel is 8 here
    int pixelscount = headerStruct.BitmapWidth * headerStruct.BitmapHeight * bytesperpixel; //BitmapWidth = 296, BitmapHeight = 16, bytesperpixel = 1

    var imageArray = new byte[pixelscount]; //pixelscount = 4736 

    try //now read in the bitmap's bytes
    {
        fs.Read(imageArray, 0, pixelscount); //filestream position is 5760 after this line
    }
    catch (Exception ex)
    {
    }

    Bitmap bmp;
    using (var ms = new MemoryStream(imageArray))
    {
        try
        {
            bmp = new Bitmap(ms); //error thrown here Exception thrown: 'System.ArgumentException' in System.Drawing.dll
            //Parameter is not valid
        }
        catch (Exception ex)
        {
        }
    }
}

On the line bmp = new Bitmap(ms), I get a System.ArgumentException in System.Drawing.dll. My try/catch shows a Parameter is not valid. exception.

I've seen a few other questions on this site with the same error, but none of the solutions have worked from the ones I've seen.

pfinferno
  • 1,779
  • 3
  • 34
  • 62
  • Open the file with notepad. Most picture has an ascii header indicating the type of picture is in the file. Last week with similar issue I found the bytes were packed using Convert To 64 bit string. – jdweng Oct 31 '18 at 15:54
  • Would the ascii header contain some kind of constant string that I could search for in the file? Lots junk to sort through. – pfinferno Oct 31 '18 at 16:08
  • You are failing due to the Bitmap not recognizing the type of picture or you lost some of the bytes. So the first thing I usually do is to make sure the number of bytes or the original file matches what I'm sending to the bit map. Then I check the ascii header to see if the file type is one of the file types that the bitmap recognizes. Some picture types may pack multiple picture into one file and that is why you are seeing repeated ascii data. I just want to make sure we are sending the bitmap a recognizable type. Issue may be that bitmap isn't recognizing file with more than one picture. – jdweng Oct 31 '18 at 16:22
  • `new Bitmap(ms)` expects a _file in a known file format_ in that stream, not raw pixel data. You need to [build an image from the raw pixel data bytes](https://stackoverflow.com/a/43967594/395685). Also note, if this is 8bpp indexed format, you need to add a palette, which is probably included in that header data. – Nyerguds Nov 13 '18 at 14:20

0 Answers0