locked
BitmapImage.StreamSource missing RRS feed

  • Question

  • We are porting some WPF code, we use the (WPF) BitmapImage.StreamSource property to load images from within ZIP files. How to do this in Metro?

    ---bjorn


    ---bjorn
    Friday, October 7, 2011 12:14 PM

Answers

  • @Sameer.V.  If you are loading images into an Image element from your AppX package, you can do the following from XAML:

          <Image x:Name="img" Source="Images/Logo.png" Stretch="None"/>
    

    From code, you can do the following:

          Uri           uri = new Uri("ms-resource:Images/SplashScreen.png");
          BitmapImage   bim = new BitmapImage(uri);
    
          img.Source = bim;
    

    @Bjorn - is your issue extracting the image from the ZIP archive or is it getting it to display in an Image element.  If you already have it extracted as a stream, then you can do the following to display in an Image element:

          BitmapImage                   bim = new BitmapImage();
          InMemoryRandomAccessStream    ras = new InMemoryRandomAccessStream();
    
          // Write stream into ras
          Stream                        stream = ras.GetOutputStreamAt(0).AsStream();
          // "yourstream" would be a stream to your image
          await yourstream.Content.CopyToAsync(stream);
    
          bim.SetSource(ras);
    
          img.Source = bim;
    

    Joe

    Monday, October 10, 2011 4:20 PM

All replies

  • Oh, we have exact same problem. I want to be able to load images from xaml package. I'm looking following 2 approaches.

    1. Use Converters, so we pass URI to the Image class. And converter will extract the package/zip name from the URI and open the image and return BitmapSource.

    2. Write implementation of BitmapSource (see http://blogs.msdn.com/b/dwayneneed/) to read resource from zip or any other resource.

    P.S. I found BitmapImage iteself to be buggy, often time not showing any images.


    Sameer V.
    Monday, October 10, 2011 2:50 PM
  • @Sameer.V.  If you are loading images into an Image element from your AppX package, you can do the following from XAML:

          <Image x:Name="img" Source="Images/Logo.png" Stretch="None"/>
    

    From code, you can do the following:

          Uri           uri = new Uri("ms-resource:Images/SplashScreen.png");
          BitmapImage   bim = new BitmapImage(uri);
    
          img.Source = bim;
    

    @Bjorn - is your issue extracting the image from the ZIP archive or is it getting it to display in an Image element.  If you already have it extracted as a stream, then you can do the following to display in an Image element:

          BitmapImage                   bim = new BitmapImage();
          InMemoryRandomAccessStream    ras = new InMemoryRandomAccessStream();
    
          // Write stream into ras
          Stream                        stream = ras.GetOutputStreamAt(0).AsStream();
          // "yourstream" would be a stream to your image
          await yourstream.Content.CopyToAsync(stream);
    
          bim.SetSource(ras);
    
          img.Source = bim;
    

    Joe

    Monday, October 10, 2011 4:20 PM
  • Hi Joe,

    We have the streams so your code should work just fine. Will try it later today.

    Thanks,

    ---bjorn
    Bjorn Backlund
    http://www.linkedin.com/pub/bjorn-backlund/18/71a/57a
    CTO, AppAvior Media Corporation
    App Discovery - App Authoring - App Trends
    AppAviorMedia.com


    ---bjorn
    Monday, October 10, 2011 4:23 PM
  • Hi Joe,

    This should work just fine. Thanks.


    Sameer V.
    Monday, October 10, 2011 5:23 PM
  • Hello Joe,

    Followup.

    Our stream is a memory stream (has no Content.CopyToAsync)

    Writing into the stream returned by ras.GetOutputStreamAt hangs the program...

    ---bjorn

     


    ---bjorn
    Monday, October 10, 2011 8:23 PM
  • Here's a sample below.  This is a complete sample but you should be able to use your stream in place of the "stream" variable below.

          BitmapImage                 bim = new BitmapImage();
          StorageFile                 sf = await Package.Current.InstalledLocation.GetFileAsync(@"Images\SplashScreen.png");
          IInputStream                iis = await sf.OpenForReadAsync();
          byte[]                      buffer = new byte[1024];
    
          InMemoryRandomAccessStream  ras = new InMemoryRandomAccessStream();
          Stream                      stream = iis.AsStream();
          Stream                      os = ras.GetOutputStreamAt(0).AsStream();
          int                         count;
    
          while ((count = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
          {
            await os.WriteAsync(buffer, 0, count);
          }
    
          bim.SetSource(ras);
          img.Source = bim;
    

     Joe

    Monday, October 10, 2011 10:23 PM