wpf - Converter Uri to BitmapImage - downloading image from web -


i have problem converter uri bitmapimage. uri url of image on web. use converter on item in listbox.

i download image webpage , create stream bitampimage

problem if listbox consist 100 - 250 items, app freeze, try call webrequestmethod in thread don’t work.

here root part of code:

private static bitmapimage getimgfromazet(int sex, uri imguri) {     try     {         if (imguri == null)         {             if (sex == (int)sex.man)             {                 return new bitmapimage(new uri(@"pack://application:,,,/spirit;component/images/defaultavatars/man.jpg",                         urikind.relativeorabsolute));             }             else             {                 return new bitmapimage(new uri(@"pack://application:,,,/spirit;component/images/defaultavatars/woman.jpg",                         urikind.relativeorabsolute));             }         }         else         {             bitmapimage image = null;              task.factory.startnew(() =>               {                   webrequest webrequest = webrequest.createdefault(imguri);                   webrequest.contenttype = "image/jpeg";                   webresponse webresponse = webrequest.getresponse();                    image = new bitmapimage();                   image.createoptions = bitmapcreateoptions.none;                   image.cacheoption = bitmapcacheoption.onload;                   image.begininit();                   image.streamsource = webresponse.getresponsestream();                   image.endinit();                   return image;                    //((system.action)(() =>                   //{                     //    //webresponse.close();                    //})).onuithread();                });              return image;         }     }     catch (exception)     { 

//default return new bitmapimage(new uri(pokecurl.avatar,urikind.relativeorabsolute)); } }

my aim download image web, create bitamimage object him , return source of image control, need avoid app freezing. problem if close webresponse broke code.

edited:

i try this:

bitmapimage image; webrequest req = webrequest.createdefault(imguri); req.contenttype = "image/jpeg";  using (var res = req.getresponse()) {         image = new bitmapimage();         image.createoptions = bitmapcreateoptions.none;         image.cacheoption = bitmapcacheoption.onload;         image.begininit();         image.urisource = imguri;         image.streamsource = res.getresponsestream();         image.endinit(); } 

but somewhere must bug, code broken.

any advice?

binding converter executed on ui thread. start other thread in convert method (as need feedback thread) have wait until completes, thereby you're blocking app.

in order solve problem, example, use binding.isasync property:

public class listitemviewdata {    private readonly uri _uri;    private readonly sex _sex;      listitemviewdata(uri uri, sex sex)    {       this._uri = uri;       this._sex = sex;    }     public bitmapsource image    {               {            // synchronous webrequest         }    } } 

usage in xaml (inside datatemplate of listbox item):

<image source="{binding path=image, isasync=true}"/> 

edited

i've dived bitmapimage class , have found out has pretty ctor uri parameter, works asynchronously.

so shouldn't execute webrequest yourself. this:

public object convert(object value, type targettype, object parameter, cultureinfo culture) {     var uri = (uri)value;     return new bitmapimage(uri) { cacheoption = bitmapcacheoption.none }; }  

edited 2

your view data class.

public class listitemviewdata : inotifypropertychanged {     public listitemviewdata(uri uri)     {         this._uri = uri;     }  private readonly uri _uri; public uri uri {          {         return this._uri;      } }  private bitmapsource _source = null; public bitmapsource image {         {         return this._source;     }     set      {         this._source = value;         this.onpropertychanged("image");     } }  public event propertychangedeventhandler propertychanged; private void onpropertychanged(string p) {     var pc = this.propertychanged;     if (pc!=null)     {          pc(this, new propertychangedeventargs(p));     } } } 

helper, executes images downloading:

public static class webhelper {     public static stream downloadimage(uri uri, string savepath)     {         var request = webrequest.create(uri);         var response = request.getresponse();         using (var stream = response.getresponsestream())         {             byte[] buffer = new byte[response.contentlength];             int offset = 0, actuallyread = 0;                         {                 actuallyread = stream.read(buffer, offset, buffer.length - offset);                 offset += actuallyread;             }             while (actuallyread > 0);             file.writeallbytes(savepath, buffer);             return new memorystream(buffer);         }     }    } 

when filling model - should start separate thread, download files , set images source.

this._listitems.add(new listitemviewdata(new uri(@"http://lifeboat.com/images/blue.ocean.jpg"))); //... var sc = synchronizationcontext.current; new thread(() => {     foreach (var item in this._listitems)     {          var path = "c:\\folder\\"+item.uri.segments.last();         var stream = webhelper.downloadimage(item.uri, path);          sc.send(p =>             {                 bitmapimage bi = new bitmapimage();                 bi.begininit();                 bi.streamsource = (stream)p;                 bi.endinit();                 item.image = bi;             }, stream);     } }).start(); 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -