1.11M Members

How to Write a Byte [ ] Verbatim to a File

 
0
 

Alright so here's a good one for you guys.

Long story short I am trying to create a .bmp file out of a byte []. Now I have code that will convert a byte [] to an Image that I then save to a file, but this doesn't work. Why? Well because the header of the Bitmap has been altered. Originally a bitmap file has a header that is 54 bytes long, I however, have altered the header, adding an addition 4 bytes, altereing the "Offset number of bytes before image starts" 4 bytes (which originally is 54 bytes) to 58 bytes.

Okay so I have finally gotten this all worked out, the problem is when I loaded in the file I saved, I noticed the 4 extra bytes I added were missing from my header (I should point out I feel pretty confident that I am allowed to added these extra 4 bytes as I tweaked the header to support it).

With this problem I decided maybe I need to manually write all the bytes in my array to a file. This should work just fine as I have the complete header pre-built and everything else is good.

I have tried using items like "File.WriteAllBytes" and "File.ReadAllBytes", however this doesn't work. First of all the .BMP image that is saved won't display correctly (like view it in an image viewer, it complains it can't display it) and when I read it in, not only do I find my header missing from the file, I also find more bytes missing (specifically 0 values).

So my question is how can I write my Byte [] byte for byte to a file I specify. Like a clean slate, I don't want any excess header data getting mixed in.

Thanks for any help

 
0
 

what size do you specify when initializing the byte[] ?
it should be a simple for loop to iterate through the byte[] and writing it to a file .

 
0
 

The byte []'s size can vary, but I can do something Length property to determine the size.

My question what do I use? I see all these writers, but I wonder which one will give me the results I want. I am looking into BinaryWriter right now (not sure of though).

I also want to point out I found what originally what was removing my header bytes. The code below is altering the header. So I am probably going to have to scratch this as I can't find a way to override the functionality (it's that image.Save)

public static byte [] ImageToByte (Image image, System.Drawing.Imaging.ImageFormat format) //converts an image to a byte []
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
        image.Save(memoryStream, format);
        byte [] imageBytes = memoryStream.ToArray();

        return imageBytes;
    }
}
 
0
 

Update, I might have had a fix, all of a sudden it seems like the "File.ReadAllBytes" and "File.WriteAllBytes" are working all of a sudden. Not sure what it refused to originally.

Of course if people have better items to use please let me know

 
0
 

How are you writing the file? If you're using File.OpenOrCreate this won't truncate the file. If your new file size is smaller (I don't know what you're doing to the bitmap even though your header is larger) then you'll get junk at the end of the file that was there before you started writing to it.

EDIT: Just clicked you're using File.WriteAllBytes. I don't know if this truncates or not. Will test now...

EDIT 2: Yes it truncates. So I don't know why it would be failing before unless the data you were putting into your header was incorrect?

If you're getting 0 value bytes, it would be good to check what image.Save is outputting and whether or not your header informatin is being inserted, or if you're overwriting by mistake.

 
0
 

Well I have no clue what was going on but the WriteAllBytes and ReadAllBytes have shown 100% success all of a sudden, doing exactly what I need it to do.

Question Answered as of 6 Months Ago by brite_a and Ketsuekiame
Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article