Saving an array of bytes

Megalith

Well-known member
Joined
Aug 21, 2006
Messages
66
Programming Experience
10+
i have an array of 16 bit bytes i wish to save to a file. I have a savefiledialog in my application so it can be placed anywhere the end user requires with the default extension added. How do i save the array though? with an image it is simply a case of doing image.save("c:/mylocation/mypic.bmp") kind of thing. no such procedure exists with arrays. looking in my textbooks drew no light nor did a search on the internet where the advice given on one website for a similar question was:-

' Use the System.Windows.Forms.SaveFileDialog class. If you're using
VS.NET, you shoud be able to find it in the toolbox. Just set its
properties, then call ShowDialog at runtime.' lol.

I presume all the SaveFileDialog does is open the file for writing too :confused:
 
SaveFileDialog give you a filename. FileStream can write the bytes.
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fs [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IO.FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None)[/SIZE]
[SIZE=2]fs.Write(bytes, 0, bytes.length)[/SIZE]
[SIZE=2]fs.Close()[/SIZE]
 
thats great JohnH, things havent changed that much with basic really ;)

one slight problem my array is 16 bit not 8 so i get a value cannot be converted error.

looks like i will have to use bitconverter on it or rewrite the array generation code
 
A byte always was 8 bits, if you're thinking about bits per pixel (bpp) that is another thing.
 
the array i created is a short word in UShort containing some values from 0 to 65535 however converting it to bytes proved relatively easy as not many 16 bit values are used, the ones that are i have stored as 2 bytes by adding a sub to divide up into bytes and swap them it works great btw thanks for the advice
 
Back
Top