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. }  

47 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
    Replies
    1. This is a useful solution for improving application responsiveness by loading images on a background thread in Silverlight for Windows Phone 7. The explanation clearly highlights the challenges of working with UI-bound classes such as BitmapImage and WriteableBitmap, while providing a practical workaround using ThreadPool and dispatcher callbacks. Techniques like these are essential for building smooth and responsive user experiences in Android Projects for Final Year and other mobile application development environments.

      Delete
    2. I also appreciate the clean separation between image loading logic and UI updates through events and asynchronous processing. Managing resource-intensive operations in the background is a key aspect of modern application development, helping to improve performance and user satisfaction. The concepts discussed here are equally valuable for developers working on Web Development Projects where efficient handling of media content is important.

      Delete
  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. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. Get the best catering services in chennai online

    ReplyDelete
  14. 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
  15. Interesting explanation about loading images using a background thread. In many UI frameworks like Silverlight or mobile platforms, image-related classes often inherit from UI-thread-bound objects, which means they cannot be safely accessed from worker threads. Because of that, developers usually create the bitmap on the UI thread and perform heavy work like decoding or downloading in a background thread. This approach helps keep the interface responsive while the image loads asynchronously. For developers sharing tutorials like this online, learning outreach strategies through a digital marketing course can help their technical content reach a broader audience.

    ReplyDelete
  16. Helpful explanation about loading images in a background thread. In Android development, performing heavy operations like network image loading on the main UI thread can cause the app to freeze or become unresponsive. Using background threads or asynchronous tasks allows developers to fetch images without blocking the interface and then update the UI once the data is ready. ) Developers who want to strengthen their programming skills can explore courses such as **Java Course Chennai**, **Python Course**, **Data Science Course**, **Data Analyst Course**, **Full Stack Development Course**, and **UI/UX Course** offered by Login360.

    ReplyDelete
  17. For anyone looking to build a strong foundation in design and user experience along with technical skills, this detailed resource on
    UI/UX design course syllabus can be really helpful. It covers essential concepts needed to grow in today’s digital space.

    Looking forward to more valuable content like this!

    ReplyDelete
  18. This really helped me understand better. Also exploring digital marketing training in chennai.

    ReplyDelete
  19. Nice article on loading images efficiently in background threads—very helpful for improving app performance without blocking the UI. Check out this UI UX design course online to enhance your design skills too!

    ReplyDelete
  20. Very informative! For UI/UX skills, the Figma UI/UX Course is excellent. Developers can also try the Full Stack Developer Course in Chennai.

    ReplyDelete
  21. This article was worth reading till the end. In the digital world, backend development and user interface planning go hand in hand, so a Python course in Kochi along with a UI/UX design course syllabus is a perfect combo.

    ReplyDelete
  22. Thanks for sharing such detailed insights. It’s helpful for anyone trying to understand the topic better. I’ve also been looking into Data Science Course with Placement and Digital Marketing Course in Kochi.

    ReplyDelete
  23. Helpful post! The explanation about loading images in a background thread on Android was clear and practical. Performance optimization tips like these are really useful for Android developers trying to create smoother and faster apps.
    Also, for anyone interested in learning analytics and tech skills, check out
    Data Analytics Beginners Course |
    Data Analytics Course in Coimbatore

    ReplyDelete
  24. Very interesting and well-structured blog. The information shared here is valuable and easy to understand for readers of all levels.
    Anyone interested in analytics can explore this Data Analytics Course in Coimbatore.
    For beginners, this Data Analytics Beginners Course offers a great starting point.

    ReplyDelete
  25. Awesome post! The information shared here is very practical and educational. Readers looking for career-focused learning can check Machine Learning Engineer and Digital Marketing Training Institute in Coimbatore.

    ReplyDelete