Skip to main content

Microsoft Silverlight

Answered Question writeablebitmap load imageRSS Feed

(0)

mexican
mexican

Member

Member

164 points

713 Posts

writeablebitmap load image

To use writeable bitmap I need to wait until an image is loaded .

I am having all sorts of problems detecting if an image is loaded as when I load dynamic images from a class .

Is there another way I can detect if an image is loaded.

I am trying to use writeable download  after the image is loaded and my method isnt working

 

 uu = New BitmapImage(url)
        uu.UriSource = url
        img.Source = uu
        Canvas.SetLeft(img, _x)
        Canvas.SetTop(img, _y)
        'img.SetValue(Canvas.ZIndexProperty, 13)
        Canvas.SetZIndex(img, 10)
        canvas1.Children.Add(img)
        AddHandler uu.DownloadProgress, AddressOf uu_downloadProgress
  
    Public Sub uu_downloadProgress(ByVal sender As Object, ByVal e As DownloadProgressEventArgs)
        Dim i As Integer
        Dim c As Color
        Dim myuu As BitmapImage

        myuu = sender
                If e.Progress = 100 Then
                    img.Height = img.ActualHeight
            img.Width = img.ActualWidth

            wb = New WriteableBitmap(myuu)
 

ksleung
ksleung

Contributor

Contributor

5396 points

1,028 Posts

Re: writeablebitmap load image

Check my response in the following thread: http://forums.silverlight.net/forums/t/112642.aspx

Basically if you have a stream fully ready, a WriteableBitmap constructed from the stream will be immediately ready.  Not sure how to map to your VB code so you'll have to work that out.

mexican
mexican

Member

Member

164 points

713 Posts

Re: Re: writeablebitmap load image

Do I need a filestream or a download-completed event. there seems a few ways to get an image so i am confused

ksleung
ksleung

Contributor

Contributor

5396 points

1,028 Posts

Re: Re: Re: writeablebitmap load image

Yeah there are multiple ways to get an image.  The sure way (not necessarily the only way) is that if you have handle to a *completed* stream, and instantiate the WriteableBitmap from said stream.

I assume you can do this instantiation during the download completed event, using the stream provided.

mexican
mexican

Member

Member

164 points

713 Posts

Re: Re: Re: Re: writeablebitmap load image

why do i need to use streams as opposed to loading from a local drive on a server? I just have been loading a bitmap image using Uri and assign the source to an image. I have read much about loading images from streams as this seems uncommon.

ksleung
ksleung

Contributor

Contributor

5396 points

1,028 Posts

Re: Re: Re: Re: Re: writeablebitmap load image

Well, if it works for you that's fine.  Getting information about an image (width and height and whether it is ready) can be tricky.  As I said what I proposed is a way to do it (that I know works), not necessarily the only way.

mexican
mexican

Member

Member

164 points

713 Posts

Re: Re: Re: Re: Re: Re: writeablebitmap load image

hi,

My way isnt working as I dont always see the image loaded , so using steams is more stable? I need to see a simple demonstration of streams and see where you detect the image loaded so I can use writeablebitmap.

ksleung
ksleung

Contributor

Contributor

5396 points

1,028 Posts

Re: Re: Re: Re: Re: Re: Re: writeablebitmap load image

Why don't you read the other thread in detail?  I already provided some code there.  I don't think it is too much to ask you to piece the puzzles together otherwise it would be called tube-feeding.

mexican
mexican

Member

Member

164 points

713 Posts

Re: Re: Re: Re: Re: Re: Re: Re: writeablebitmap load image

OK I have looked at your thread and it is taking me a while to make sense of it...

1)To load an image fro a writeablebitmap I need a stream as downloadProgress doesnt work?

The issue is going from one method to another with little information is taking much of my time.

All I want to do is simply load 2 images and detect to see when it has loaded so I can use writeablebitmap  in vb.net. My way doesnt always load the image and I am really frustrated with this.


 

msalsbery
msalsbery

Contributor

Contributor

2056 points

379 Posts

Re: writeablebitmap load image

mexican:
Is there another way I can detect if an image is loaded.
 

 

BitmapImage now has ImageOpened/ImageFailed events.  ImageOpened is specifically for getting the dimensions of the loaded image instead of the old kludgy methods. From an ImageOpened handler, you should be able to do something like this:

img.Width = bitmapimg.PixelWidth

 
Also, if you're having intermittent  problems getting the event fired, maybe try subscribing to the event BEFORE you set the  BitmapImage.UriSource property...

Mark Salsbery
Microsoft MVP - Visual C++

mexican
mexican

Member

Member

164 points

713 Posts

Re: Re: writeablebitmap load image

hi,

yes imageOpened it worked fine .

q1)Instead of opening another thread, how do i detect if i have anumber of images downloaded over a few classes? would i use a imageOpened event for each and have some kind of global variable in a variable to detect if all images are loaded?How can i do this?

 

msalsbery
msalsbery

Contributor

Contributor

2056 points

379 Posts

Re: Re: writeablebitmap load image

mexican:
how do i detect if i have anumber of images downloaded over a few classes?
 


I'm not quite picturing the specifics of what you're trying to do....do you have some more info or pseudocode you can provide, such as where the downloads originate from, what you want to do with the BitmapImages once they are opened, etc?

 

Mark Salsbery
Microsoft MVP - Visual C++

mexican
mexican

Member

Member

164 points

713 Posts

Re: Re: Re: writeablebitmap load image

 I want to display a splash screen until all images are loaded. how do i detect when all images are loaded given images are loaded over many classes?

msalsbery
msalsbery

Contributor

Contributor

2056 points

379 Posts

Re: Re: Re: writeablebitmap load image

mexican:
I want to display a splash screen until all images are loaded. how do i detect when all images are loaded given images are loaded over many classes?
 


First thing that comes to mind is

Create some singleton/static monitor class that keeps a collection of objects and has a finished event.

Each class object that will be loading images will register itself with the monitor object.

Show the splash screen - the splash screen could subscribe to the finished event on the monitor object.

As each image-loading class completes its loading it can notify the monitor object.

When all registered class objects on the monitor object have notified the object of their completion, the finished event could be raised, at which point the splash screen could remove itself or be removed.


Mark Salsbery
Microsoft MVP - Visual C++

mexican
mexican

Member

Member

164 points

713 Posts

Re: Re: Re: Re: writeablebitmap load image

Hi,

I was hoping for a quick method silverlight could provide but thanks for the reply. Sound like i need to use logic where when objects are loaded then simply monitor this for all classes.

 

 

msalsbery
msalsbery

Contributor

Contributor

2056 points

379 Posts

Re: Re: Re: Re: writeablebitmap load image

I certainly never mean to imply my ideas are the only way to do things.  That's just what I would do.  I can't imagine the framework being able to track all my asynchronous downloads, but I also hardly know the entire framework :)

 
For what it's worth, a class like I described should be relatively easy to implement.  Just remember, if you do something like I described, make sure you use thread synchronization when accessing the object if your load-completed events can occur on different threads!


Mark Salsbery
Microsoft MVP - Visual C++

mexican
mexican

Member

Member

164 points

713 Posts

Re: Re: Re: Re: Re: writeablebitmap load image

Hi,.

>> make sure you use thread synchronization when accessing the object if your load-completed events can occur on different threads!

I haven't done this before so can you explain this.

 

msalsbery
msalsbery

Contributor

Contributor

2056 points

379 Posts

Answered Question

Re: Re: Re: Re: Re: writeablebitmap load image

mexican:
I haven't done this before so can you explain this.
 


Explaining multithreading and associated synchronization is a big topic, but the synchonization info here is a place to start (particularly the "Locking" section applies to what I was referring to):  Synchonization Primitives

 

 In the context of my comment and proposed solution, you just need to be mindful of using a static/singleton object from potentially multiple threads.  If you're only accessing the object from a single thread (e.g. the UI thread) then no worries.

 

Mark Salsbery
Microsoft MVP - Visual C++

mexican
mexican

Member

Member

164 points

713 Posts

Re: Re: Re: Re: Re: writeablebitmap load image

I will close this thread as I have another which is asking a similar question.

 

thanks for the help

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities