c# - Images not appearing on WPF form when loading asynchronously -
i'm attempting display (in listbox custom datatemplate) series of bitmapsource frames (thumbnails) extracted multi-page tiff image. when process tiff on ui thread, , either directly add images listbox's item collection or add them bound observablecollection, show fine in list. however, when trying load each thumbnail asynchronously (either backgroundworker or using asynchronous tasks), see behavior can't work out:
- the first thumbnail loads expected
- the next, , subsequent thumbnails items in list (i see border), shows blank image. shows correct number of items, no images after first.
i've played around trying freeze thumbnails (no good), trying send them ui thread , adding them collection there via worker's reportprogress (no good), can't seem them show.
working on ui thread (where syncimages observablecollection bound listbox, , onpropertychanged handles notification event):
private void loadsynchronous() { stream imagestreamsource = new filestream(imagepath, filemode.open, fileaccess.read, fileshare.read); var decoder = bitmapdecoder.create(imagestreamsource, bitmapcreateoptions.preservepixelformat, bitmapcacheoption.default); foreach (var frame in decoder.frames) { //frame.freeze(); //tried no effect. syncimages.add(frame); } onpropertychanged("syncimages"); }
not working (this example adds frames directly list, i've tried via binding no difference in result):
private void loadasync(object sender, doworkeventargs e) { stream imagestreamsource = new filestream(imagepath, filemode.open, fileaccess.read, fileshare.read); var decoder = bitmapdecoder.create(imagestreamsource, bitmapcreateoptions.preservepixelformat, bitmapcacheoption.default); foreach (var frame in decoder.frames) { // frame.freeze(); (sender backgroundworker).reportprogress(0, frame); } onpropertychanged("asyncimages"); } private void reportasyncprogress(object send, progresschangedeventargs e) { var frame = (bitmapsource) e.userstate; lbasynchronous.items.add(frame); }
hoping can shed light on going on here. know code works extract frames, in async example must loaded, seems ui thread can't access properties of source hold image data display them on form (which why tried freezing).
any thoughts appreciated!
example image: http://i.imgur.com/75wmkms.png
@clemens answer comment on original question provided solution. ensuring file stream being closed responsibly , changing bitmapcacheoption
onload
shows each image in asynchronous load.
the final code asynchronous load looks like:
private void loadasync(object sender, doworkeventargs e) { bitmapdecoder decoder; using (stream imagestreamsource = new filestream(imagepath, filemode.open, fileaccess.read, fileshare.read)) { decoder = bitmapdecoder.create(imagestreamsource, bitmapcreateoptions.preservepixelformat, bitmapcacheoption.onload); } foreach (var frame in decoder.frames) { frame.freeze(); (sender backgroundworker).reportprogress(0, frame); } } private void updateasync(object send, progresschangedeventargs e) { syncimages.add((bitmapsource)e.userstate); onpropertychanged("syncimages"); }
Comments
Post a Comment