Previewing WMP files while downloading them RRS feed

  • Question

  • Hello.

    I would like to load a WMP file to a WPF client from a server.
    I read that this format can preview data while the image is being downloaded, a preview that gets better and better as more data is downloaded.

    I tried to simulate this with a very simple WPF application:
    private void button1_Click(object sender, RoutedEventArgs e) {
                MemoryStream MS = new MemoryStream();
                int read = 0;
                byte[] buffer = new byte[4*1024]; // 4k buffer 
                FileStream FS = new FileStream(@"E:\Im.wdp", System.IO.FileMode.Open);
                WmpBitmapDecoder Dec = new WmpBitmapDecoder(MS, BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand);
                while (true) {
                    read = FS.Read(buffer, 0, buffer.Length); // read from the file
                    if (read == 0) break; // EOF
                    MS.Write(buffer, 0, buffer.Length); // write to the memory
                    ImageSource Prev = null;
                    try {
                        Prev = Dec.Preview;
                    } catch {
                        // Throws an exception...
                    if (Prev != null) {
                        image1.Source = Dec.Preview; // image1 is the Image object
                image1.Source = Dec.Frames[0];
    Obviously, the above code fails...
    1. Why can't I create an instance on WmpBitmapDecoder with an empty stream? If i read ~16 bytes to the memory stream before creating the instance it seems to work.
    2. Documentation states that is Preview doesn't exists then null is returned. I keep getting an exception and not null;
    Any ideas what I'm I doing wrong?

    thanks for your help


    Thursday, July 23, 2009 1:53 PM