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

Saving Stream as Image.

P: n/a
Hi Gurus,

I have an Application, in which
1) need to post data to a URL(Remote Server), by using HTTPRequest.
2) get the Image data in the form of Stream in Response.
3) need to save this stream as a Image file on the local machine.

Giving some code snippet below.

RequestAPI = CType(WebRequest.Create(URLName), HttpWebRequest)
RequestData = ImageName
RequestAPI.ContentType = "application/x-www-form-urlencoded"
RequestAPI.Method = "POST"
StoreRequestStream = New StreamWriter(RequestAPI.GetRequestStream())
StoreRequestStream.Write(RequestData)

'Close the StreamWriter Object
StoreRequestStream.Close()
StoreResponse = CType(RequestAPI.GetResponse(), HttpWebResponse)
Dim ImgStream As Stream
Dim StremaLen As Long

ImgStream = StoreResponse.GetResponseStream()
StremaLen = StoreResponse.ContentLength
'**** For Saving Image File ****
WriteImgFile(ImgStream, StremaLen)

Public Sub WriteImgFile(ByVal ImgStream As Stream, ByVal RespLen As
Long)
Dim BinReader As BinaryReader
BinReader = New BinaryReader(ImgStream)

Dim BinWriter As BinaryWriter
Dim objDestFile As FileStream
Dim ImgByteArr() As Byte

ImgByteArr = BinReader.ReadBytes(CInt(RespLen))

Try
objDestFile = New FileStream("D:/ImageTest/Test.jpg",
FileMode.Create, FileAccess.Write)
BinWriter = New BinaryWriter(objDestFile)
BinWriter.Write(ImgByteArr)
Catch ex As IOException
End Try
End Sub

The problem is, I get StoreResponse.ContentLength as -1, which prevents
me from saving the Stream data as File. Actually Image resides on the
server. How this can be achieved ?

Any help in this regard would be appreciated.

Thanking you in anticipation.

Regards,

Umesh.

Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.