Skip to main content
Home Forums General Silverlight Getting Started writeablebitmap load image
18 replies. Latest Post by mexican on November 4, 2009.
(0)
mexican
Member
164 points
713 Posts
11-01-2009 4:48 PM |
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
Contributor
5396 points
1,028 Posts
11-01-2009 6:24 PM |
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.
11-01-2009 7:34 PM |
Do I need a filestream or a download-completed event. there seems a few ways to get an image so i am confused
11-01-2009 9:54 PM |
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.
11-01-2009 10:15 PM |
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.
11-01-2009 11:05 PM |
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.
11-01-2009 11:20 PM |
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.
11-02-2009 12:04 AM |
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.
11-02-2009 1:13 AM |
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
2056 points
379 Posts
11-02-2009 3:46 PM |
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...
11-02-2009 5:34 PM |
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?
11-02-2009 6:00 PM |
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?
11-02-2009 7:37 PM |
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?
11-02-2009 8:03 PM |
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.
11-03-2009 12:46 AM |
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.
11-03-2009 4:32 PM |
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!
11-04-2009 3:14 AM |
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.
11-04-2009 2:50 PM |
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.
11-04-2009 5:10 PM |
I will close this thread as I have another which is asking a similar question.
thanks for the help