c# - Image to string -
i have news list, contain thumbnails:
i'm getting json server, means pictures - links
my code inserting pictures:
foreach (article article in newslist.result.articles) { newslistboxitem nlbi = new newslistboxitem(); nlbi.title.text = article.title; nlbi.date.text = us.unixdatetonormal(article.date.tostring()); nlbi.id.text = article.id.tostring(); if (article.imageurl != null) { bitmapimage image = new bitmapimage(new uri(article.imageurl)); nlbi.thumbnail.source = image; } newslistbox.items.add(nlbi); }
if enter application in offline mode - wont show up, need save them, prefered method - string!
which means need convert them base64:
bitmapimage image = new bitmapimage(new uri(article.imageurl)); byte[] bytearray = null; using (memorystream ms = new memorystream()) { writeablebitmap wbitmp = new writeablebitmap(image ); wbitmp .savejpeg(ms, wbitmp.pixelwidth, wbitmp.pixelheight, 0, 100); ms.seek(0, seekorigin.begin); bytearray = ms.getbuffer(); } string str = convert.tobase64string(bytearray);
code fails nullreferenceexception
mistake? code fails on line:
writeablebitmap wbitmp = new writeablebitmap(image);
error:
an exception of type 'system.nullreferenceexception' occurred in system.windows.ni.dll not handled in user code
i tried use image project:
bitmapimage image = new bitmapimage(new uri("theme/menubutton.png",urikind.relative));
but still fails nullref - know image exists, if nul wouldn't see in imagebox
the problem image needs loaded. can try saving image first timem shown:
foreach (article article in newslist.result.articles) { newslistboxitem nlbi = new newslistboxitem(); nlbi.title.text = article.title; nlbi.date.text = us.unixdatetonormal(article.date.tostring()); nlbi.id.text = article.id.tostring(); if (article.imageurl != null) { bitmapimage image = new bitmapimage(new uri(article.imageurl)); image.imageopened += (s, e) => { byte[] bytearray = null; using (memorystream ms = new memorystream()) { writeablebitmap wbitmp = new writeablebitmap(image ); wbitmp .savejpeg(ms, wbitmp.pixelwidth, wbitmp.pixelheight, 0, 100); bytearray = ms.toarray(); } string str = convert.tobase64string(bytearray); }; nlbi.thumbnail.source = image; } newslistbox.items.add(nlbi); }
Comments
Post a Comment