By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,822 Members | 1,003 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,822 IT Pros & Developers. It's quick & easy.

saving an image from a byte array

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies

P: n/a
James,

Have a look at this sample on our website.

http://www.windowsformsdatagridhelp....7-e68d238952a5

I hope this helps,

Cor
Nov 21 '05 #2

P: n/a

"JamesB" <ja****@jmb.is-a-geekWhoDoesntLikeSpam.com> wrote in message
news:42***********************@news.zen.co.uk...
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

Nov 21 '05 #3

P: n/a

"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:eb*************@TK2MSFTNGP09.phx.gbl...


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
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.