saving an image from a byte array

J

JamesB

I am saving image files from my website using the downloaddata on the web
client. This gives me a byte array, which I am then saving to a file with
the following code:

' Create the new, empty data file.
If File.Exists(ImgDest) Then
Console.WriteLine("{0} already exists!", ImgDest)
Return
End If
Dim fs As New FileStream(ImgDest, FileMode.CreateNew)
' Create the writer for data.
Dim w As New BinaryWriter(fs)
' Write data to Test.data.
w.Write(ImgArray)
w.Close()
fs.Close()

This works, in that I end up with the file(s) on my machine, but if I try
and open them they come up as invalid. Any clues? Am I missing a step to do
with image headers or something?
James.
 
M

Mythran

JamesB said:
I am saving image files from my website using the downloaddata on the web
client. This gives me a byte array, which I am then saving to a file with
the following code:

' Create the new, empty data file.
If File.Exists(ImgDest) Then
Console.WriteLine("{0} already exists!", ImgDest)
Return
End If
Dim fs As New FileStream(ImgDest, FileMode.CreateNew)
' Create the writer for data.
Dim w As New BinaryWriter(fs)
' Write data to Test.data.
w.Write(ImgArray)
w.Close()
fs.Close()

This works, in that I end up with the file(s) on my machine, but if I try
and open them they come up as invalid. Any clues? Am I missing a step to
do with image headers or something?
James.

Since it's binary data you are writing, I would use the BinaryWriter. But,
since it's an image, I would actually read the byte array into a
MemoryStream, create an Image class instance, then call it's "Save" method.

Mythran
 
J

JamesB

Mythran said:
Since it's binary data you are writing, I would use the BinaryWriter. But,
since it's an image, I would actually read the byte array into a
MemoryStream, create an Image class instance, then call it's "Save" method.


Thanks for both replies- turns out after more checking my method did
actually work... there was a bug in the way I was building my url from other
places in the app that I hadn't spotted!
I'll do some more testing and if it does give more problems I'll look into
the suggestions you've both given.
Thanks
James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top