Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7895473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:39:07+00:00 2026-06-03T07:39:07+00:00

So, I have been working on adding BitmapImages to a ListBox for pretty much

  • 0

So, I have been working on adding BitmapImages to a ListBox for pretty much my whole work day. Here’s my code:

public class SomeClassViewModel : ViewModelBase
{
    public SomeClassViewModel(EnhanceImage image)
    {
       LoadImages();
    }

    public List<BitmapImage> ListOfImages { get; set; }

    public void LoadImages()
    {
        ListOfImages = new List<BitmapImage>();
        DirectoryInfo imageDir = new DirectoryInfo(@"..\..\Images");
        foreach (FileInfo imageFile in imageDir.GetFiles("*.jpg"))
        {
            Uri uri = new Uri(imageFile.FullName);
            ListOfImages.Add(new BitmapImage(uri));
        }
    }
}

As you can see I’m adding all the images in the folder “Images” that have the file extension “.jpg” into a list of BitmapImages called ListOfImages. Then after that I bind it to a ListBox in the XAML like so:

<ListBox ItemsSource="{Binding ListOfImages}"/>

The problem is is that when I run it, I get this error: “the calling thread cannot access this object because a different thread owns it”. Now I kind of know what I have to do from looking at this StackOverflow thread: The calling thread cannot access this object because a different thread owns it.How do i edit the image?, but I just couldn’t follow it. Could someone show me how to use this Dispatcher.Invoke?

EDIT: Right now I’m just trying to get the URI of the image to show up in a list.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:39:09+00:00Added an answer on June 3, 2026 at 7:39 am

    I figured it out and thought I would post my answer.

    Here’s what I did inside my foreach loop:

    Uri uri = new Uri(imageFile.Fullname);
    BitmapImage tmpImage = new BitmapImage(uri)
    tmpImage.Freeze();
    Action action = delegate { ListOfImages.Add(tmpImage); };
    Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
    dispatcher.Invoke(action);
    

    As you can see what I did was freeze the BitmapImage, and then delegated the action of adding the BitmapImage to the ListOfImages. Then at the end I invoked the action.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a server-side framework. Here and there I have been adding
I have this code I been working on but I'm having a hard time
I have been working on a tab menu without adding the doctype statement. It
I have some really complicated legacy code I've been working on that crashes when
i have been working on plist these days and got stuck in adding a
Have been working on this question for a couple hours and have come close
I have been working on Eclipse recently. I am fairly new to java programming,
I have been working on a Windows Mobile app for a little while now
I have been working on keeping things object oriented for my project. Currently, I'm
I have been working on this problem for 2 days now and it's an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.