.net - How do I download images (jpg) via a webclient and save to isolated storage on Windows Phone 7? -
due lack of programming experience (3 months) have been unable recreate found examples of above question. examples have found relate non wp7 silverlight, camera based saving of images, have been complex needs or have not worked. have been able download text file using instance of webclient , save isolated storage using streamwriter. need acomplish same task jpg images. below have used download text file.
===============================================================================
isolatedstoragefile mystore = isolatedstoragefile.getuserstoreforapplication(); private void gettextfile() { webclient client = new webclient(); client.downloadstringcompleted += new downloadstringcompletedeventhandler(client_downloadstringcompleted); client.downloadstringasync(new uri("http://mywebsite.com/textfile.txt")); } private void client_downloadstringcompleted(object sender, downloadstringcompletedeventargs e) { streamwriter mystreamwriter = new streamwriter(new isolatedstoragefilestream("textfile.txt", filemode.create, mystore)); mystreamwriter.writeline(e.result) mystreamwriter.close(); }
===============================================================================
i have removed few lines used handle errors etc keep simple posible.
please modify above enable me download , save jpg?
please keep simple possible confused.
thank time in advance!
resolved!===============================================================================
i managed working using information site below. http://dotnet.dzone.com/articles/operating-image-files-windows
hopefully other frustrated newbie's in future!
isolatedstoragefile mystore = isolatedstoragefile.getuserstoreforapplication(); private void getimagefile() { webclient client = new webclient(); client.openreadcompleted += new openreadcompletedeventhandler(client_openreadcompleted); client.openreadasync(new uri("http://mywebsite.com/1.jpg"), client); } void client_openreadcompleted(object sender, openreadcompletedeventargs e) { var resinfo = new streamresourceinfo(e.result, null); var reader = new streamreader(resinfo.stream); byte[] contents; using (binaryreader breader = new binaryreader(reader.basestream)) { contents = breader.readbytes((int)reader.basestream.length); } isolatedstoragefilestream stream = mystore.createfile("10.jpg"); stream.write(contents, 0, contents.length); stream.close(); }
try one, maybe helpfull you,
private void phoneapplicationpage_loaded(object sender, routedeventargs e) { httpwebrequest reqest1 = (httpwebrequest)webrequest.create(url); reqest1.begingetresponse(downloadimagecallback, reqest1); thread.sleep(1000); } void downloadimagecallback(iasyncresult result) { httpwebrequest req1 = (httpwebrequest)result.asyncstate; httpwebresponse responce = (httpwebresponse)req1.endgetresponse(result); stream s = responce.getresponsestream(); deployment.current.dispatcher.begininvoke(() => { string directory = "images"; if (!mystore.directoryexists(directory)) { mystore.createdirectory(directory); using (isolatedstoragefile myisolatedstorage = isolatedstoragefile.getuserstoreforapplication()) { using (var isofilestream = myisolatedstorage.createfile(directory + "//yourfilename.jpg")) { var wb = new writeablebitmap(bitimage); var width = wb.pixelwidth; var height = wb.pixelheight; extensions.savejpeg(wb, isofilestream, width, height, 0, 100); } } } else { using (isolatedstoragefile myisolatedstorage = isolatedstoragefile.getuserstoreforapplication()) { if (myisolatedstorage.fileexists(directory + "//yourfilename.jpg")) { myisolatedstorage.deletefile(directory + "//yourfilename.jpg"); } using (var isofilestream = myisolatedstorage.createfile(directory + "//yourfilename.jpg")) { var wb = new writeablebitmap(bitimage); var width = wb.pixelwidth; var height = wb.pixelheight; extensions.savejpeg(wb, isofilestream, width, height, 0, 100); } } } }); }
Comments
Post a Comment