Sep 6, 2011

Load Image at Background Thread in Silverlight WP7

Load image at background thread in Silverlight Windows Phone 7 application, is that possible?

Usually when you try to use BitmapImage, Image, WriteableImage in other than UI thread, you'll get exception. This is because these classes are derived from  System.Windows.Threading.DispatcherObject, which is blocked access from other than UI thread. There exists an extension to WriteableBitmap, LoadJpeg, which is works fine in thread, but you have to create WriteableBitmap object on main UI thread.


  1. using System.Windows;  
  2. using System.Windows.Media.Imaging;  
  3. using System.IO;  
  4. using System.Threading;  
  5.   
  6. namespace ImageHelpers  
  7. {  
  8.     public delegate void ImageLoadedDelegate(WriteableBitmap wb, object argument);  
  9.   
  10.     public class ImageThread  
  11.     {  
  12.         public event ImageLoadedDelegate ImageLoaded;  
  13.   
  14.         public ImageThread()  
  15.         {  
  16.         }  
  17.   
  18.         public void LoadThumbAsync(Stream src, WriteableBitmap bmp, object argument)  
  19.         {  
  20.             ThreadPool.QueueUserWorkItem(callback =>  
  21.             {  
  22.                 bmp.LoadJpeg(src);  
  23.                 src.Dispose();  
  24.                 if (ImageLoaded != null)  
  25.                 {  
  26.                     Deployment.Current.Dispatcher.BeginInvoke(() =>  
  27.                     {  
  28.                         ImageLoaded(bmp, argument);  
  29.                     });  
  30.                 }  
  31.             });  
  32.         }  
  33.     }  
  34. }  


Using scenario:

  1. ImageThread imageThread = new ImageThread();  
  2.   
  3. private void Init()  
  4. {  
  5.     imageThread.ImageLoaded += LoadFinished;  
  6. }  
  7.   
  8. void LoadFinished(WriteableBitmap bmp, object arg)  
  9. {  
  10.     Imgage1.Source = bmp;  
  11. }  
  12.   
  13. void DeferImageLoading( Stream imgStream )  
  14. {  
  15.     // we have to give size  
  16.     var bmp = new WriteableBitmap(80, 80);  
  17.     imageThread.LoadThumbAsync(imgStream, bmp, this);  
  18. }  

30 comments:

  1. With the Internet, nearly everything has been a great deal simpler. Correspondences were streamlined. Examining for articles and answers is less demanding than it was previously.best background investigation service

    ReplyDelete
  2. I am really enjoyed a lot when reading your well-written posts. It shows like you spend more effort and time to write this blog. I have saved it for my future reference. Keep it up the good work.
    honor mobile service center
    honor mobile service centre in Chennai
    honor service center near me
    honor service
    honor service centres in chennai

    ReplyDelete
  3. Learned a lot from your post and it is really good. Share more tech updates regularly.web design company in velachery

    ReplyDelete
  4. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.hadoop training institutes in bangalore

    ReplyDelete
  5. I think this is one of the most significant information for me. And i’m glad reading your article. Thanks for sharing!

    Upgrade your career Learn AWS Training from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Bangalore Training Academy Located in BTM Layout.

    ReplyDelete
  6. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck…

    Upgrade your career Learn SharePoint Developer Training in Bangalore from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Softgen Infotech.

    ReplyDelete
  7. Thank you for sharing such a nice post!

    Learn Best PEGA Training in Bangalore from Experts. Softgen Infotech offers the Best PegaTraining in Bangalore.100% Placement Assistance, Live Classroom Sessions, Only Technical Profiles, 24x7 Lab Infrastructure Support.

    ReplyDelete
  8. Way cool! Some very valid points! digital I appreciate you writing this write-up plus the rest of the site is also very good.

    ReplyDelete
  9. Well explanation with great coding knowledge. This blog gonna helpful to many. I am expecting these kind blogs in future too.
    AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  10. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck…
    AWS training in Chennai

    AWS Online Training in Chennai

    AWS training in Bangalore

    AWS training in Hyderabad

    AWS training in Coimbatore

    AWS training

    ReplyDelete
  11. Excellent blog!!! I got to know more useful information by reading your blog. Thanks for posting this blog.

    Apache Spark Training in Pune
    Spark Training Institute in Pune

    ReplyDelete
  12. Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging. After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    Salesforce Online Training

    ReplyDelete
  13. Much obliged for Sharing This Article. It is exceptionally such a lot of significant substance. I trust these Commenting records will serve to my site…

    DevOps Training in Hyderabad

    ReplyDelete
  14. Get the best catering services in chennai online

    ReplyDelete
  15. thank you for the weblog stuffed while for that reason many records. stopping by your blog helped me to make a get hold of of what i was searching out. Now my challenge has come to be as easy as ABC. Lines For Brother Day

    ReplyDelete