Preload ImageSource from URI
-
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
-
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
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.
-
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.
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?
-
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?
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.