Wednesday, 15 September 2010

c# - Displaying a picture stored in Storage file in a metroapp -



c# - Displaying a picture stored in Storage file in a metroapp -

i display content of image stored in storagefile through binding, whatever i'm trying doesn't seem work.

here 2 solutions have tested :

string img =( await competencesfolder.getfileasync(formatimagenametofilename(imagename))).path;

and returning path obtained (a total absolute path file) source property through binding, , :

bitmapimage img = await loadimage(await competencesfolder.getfileasync(formatimagenametofilename(imagename))); private static async task<bitmapimage> loadimage(storagefile file) { bitmapimage bitmapimage = new bitmapimage(); filerandomaccessstream stream = (filerandomaccessstream)await file.openasync(fileaccessmode.read); bitmapimage.setsource(stream); homecoming bitmapimage; }

returning final bitmapimage later binded property.

none of these methods works ..

do have idea?

edit : fix

here code solved problem :

bitmapimage img = new bitmapimage() { urisource = new uri( local_repository.path + "/assets/gfx/cards/" + formatimagenametofilename(imagename) + ".jpg", urikind.relativeorabsolute) };

i did mix of 2 samples above : created bitmapimage absolute uri of image (local_repository contains reference local storage : applicationdata.current.localfolder)

i still can't figure why 2 other ways failed : bind image straight uri, string or bitmapimage, , works ..

the code below shows how can utilize loadimage method in app: create blank app, add together image , button main page.

private async void button_click_1(object sender, routedeventargs e) { // load file document library. note: select document library in capabilities , declare .png file type string filename = "logo.png"; windows.storage.storagefile samplefile = await windows.storage.knownfolders.documentslibrary.getfileasync(filename); // load file local folder //windows.storage.storagefile samplefile = samplefile = await windows.applicationmodel.package.current.installedlocation.getfileasync("assets\\logo.png"); bitmapimage img = new bitmapimage(); img = await loadimage(samplefile); myimage.source = img; } private static async task<bitmapimage> loadimage(storagefile file) { bitmapimage bitmapimage = new bitmapimage(); filerandomaccessstream stream = (filerandomaccessstream)await file.openasync(fileaccessmode.read); bitmapimage.setsource(stream); homecoming bitmapimage; }

c# windows-8 microsoft-metro .net-4.5

No comments:

Post a Comment