471,616 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

Saving Stream as Image File


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())

'Close the StreamWriter Object
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))

objDestFile = New FileStream("D:/ImageTest/Test.jpg",
FileMode.Create, FileAccess.Write)
BinWriter = New BinaryWriter(objDestFile)
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.


Jul 21 '05 #1
0 1473

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Peder Y | last post: by
4 posts views Thread by CodeRazor | last post: by
5 posts views Thread by Thaynann | last post: by
2 posts views Thread by asad | last post: by
reply views Thread by Umesh | last post: by
reply views Thread by Umesh | last post: by
1 post views Thread by XIAOLAOHU | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.