rest - vb.net saving a web binary to a file -
i have web rest service trying extract image (or wav file) from. can image (jpg) via memory stream , display picture box, , save picture box file. want eliminate intermediate step of using picture box , save memory stream straight file. however, resultant file not seem jpg file. when opening throws corrupted file error.
the code have follows:
dim msguri string msguri = "http://192.168.0.1/attachment/12345/0" dim pic new picturebox() dim web_client new webclient() web_client.credentials = new networkcredential("xx", "xx") dim image_stream new memorystream(web_client.downloaddata(msguri)) pic.image = image.fromstream(image_stream) dim bm bitmap = pic.image dim filename string = "c:\temp\test.jpg" bm.save(filename, imaging.imageformat.jpeg)
and works fine.
however when use following bypass bitmap , picturebox:
using file new filestream(filename, filemode.create, system.io.fileaccess.write) dim bytes byte() = new byte(image_stream.length - 1) {} image_stream.read(bytes, 0, cint(image_stream.length)) file.write(bytes, 0, bytes.length) image_stream.close() end using
i file corrupted jpg file.
any appreciated.
terry
the webclient.downloaddata
method returns byte array. such, seems awfully silly load byte array memory stream read out again byte array, save file. of accomplished going directly first byte array file, this:
file.writeallbytes("c:\temp\test.jpg", web_client.downloaddata(msguri))
however, inefficient since stream data directly web file, this:
web_client.downloadfile(msguri, "c:\temp\test.jpg")
Comments
Post a Comment