Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Preload ImageSource from URI

Preload ImageSource from URI

Scheduled Pinned Locked Moved WPF
wpfquestioncsharpsysadmin
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ezazazel
    wrote on last edited by
    #1

    Hi folks! Is there a way to do some preloading of images in WPF? My Image has an URI as source (a URI property implementing INotifyPropertyChanged in the code behind) When I create the URI (points to a network share) and fire the NotifyPropertyChanged Event, my application freezes for a second or two. So how can I preload the image so the applicatio won't freeze? Here's some code: XAML:

    And the CB:

    public DataContextClass : INotifyPropertyChanged
    {
    private Uri image_1;
    public Uri Image_1
    {
    get
    {
    }
    set
    {
    image_1 = value;
    PropertyHasChanged("Image_1");
    }
    }

    void Timer_Elapsed(object sender, EventArgs e)
    {
    Image_1 = CreateUri(GetFilePathFromNetworkShare());
    }
    }

    Thanks in advance! eza

    P 1 Reply Last reply
    0
    • E ezazazel

      Hi folks! Is there a way to do some preloading of images in WPF? My Image has an URI as source (a URI property implementing INotifyPropertyChanged in the code behind) When I create the URI (points to a network share) and fire the NotifyPropertyChanged Event, my application freezes for a second or two. So how can I preload the image so the applicatio won't freeze? Here's some code: XAML:

      And the CB:

      public DataContextClass : INotifyPropertyChanged
      {
      private Uri image_1;
      public Uri Image_1
      {
      get
      {
      }
      set
      {
      image_1 = value;
      PropertyHasChanged("Image_1");
      }
      }

      void Timer_Elapsed(object sender, EventArgs e)
      {
      Image_1 = CreateUri(GetFilePathFromNetworkShare());
      }
      }

      Thanks in advance! eza

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      I would tend to read the data in using a background worker and marshall the image back to the UI thread once it's been read in. Whenever you see a UI that's stopped responding, you're seeing one that needs to have some threading in place.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      E 1 Reply Last reply
      0
      • P Pete OHanlon

        I would tend to read the data in using a background worker and marshall the image back to the UI thread once it's been read in. Whenever you see a UI that's stopped responding, you're seeing one that needs to have some threading in place.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        E Offline
        E Offline
        ezazazel
        wrote on last edited by
        #3

        That's true, for sure - but as a matter of fact up to now wpf is doing the black magic (inclluding the dispatching), I'm just passing an uri. Can this be done with some sort of lazy loading (heard once of that during a dev training) or other obsurce xaml tags? Or is creating an image out of the uri (including the preloading) and passing it to the xaml as imagesource the better way? Won't the problem keep existing as the dispatcher has to deal with the pictures and their transformation-rotation anyway?

        L 1 Reply Last reply
        0
        • E ezazazel

          That's true, for sure - but as a matter of fact up to now wpf is doing the black magic (inclluding the dispatching), I'm just passing an uri. Can this be done with some sort of lazy loading (heard once of that during a dev training) or other obsurce xaml tags? Or is creating an image out of the uri (including the preloading) and passing it to the xaml as imagesource the better way? Won't the problem keep existing as the dispatcher has to deal with the pictures and their transformation-rotation anyway?

          L Offline
          L Offline
          Leung Yat Chun
          wrote on last edited by
          #4

          Have you tried WritableBitmap? you can return a empty WritableBitmap, start a thread to load your URI, then when the loading is finished, update it back through Dispatcher : writeBitmap.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { //UI Thread Int32Rect outRect = new Int32Rect(0, (int)(writeBitmap.Height - height) / 2, width, height); writeBitmap.WritePixels(outRect, bits, stride, 0); })); Thats what I did for my FileToIconConverter.

          QuickZip

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups