LEAD Technologies, Inc

Working with image streams and files in WinRT (Windows Store)

For security and other reasons, WinRT (Windows Store) applications do not offer full access to the operating system's file system. Even so, there are still several ways to interact with files, whether they are on disk, in memory, over the web, in databases, virtually any location where images are stored. The following snippets show various ways to load/save images with LEADTOOLS WinRT.

  1. Using the Microsoft File Picker Dialogs:

    [C#]

                // Loading
                FileOpenPicker openPicker = new FileOpenPicker();
                openPicker.ViewMode = PickerViewMode.Thumbnail;
                openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                openPicker.FileTypeFilter.Add(".cmp");
                openPicker.FileTypeFilter.Add(".jpg");
                openPicker.FileTypeFilter.Add(".tif");
                
                StorageFile file = await openPicker.PickSingleFileAsync();
                if (null != file)
                {
                   try
                   {
                      //Open the file as a WinRT RandomAccessStream and create an ILeadStream from this RandomAccessStream
                      ILeadStream leadStream = LeadStreamFactory.Create(file);
                      using (IDisposable disposable = leadStream as IDisposable)
                      {
                         using (RasterCodecs codecs = new RasterCodecs())
                         {
                            _viewer.Image = image;
                         }
                      }
                   }
                   catch (Exception exception)
                   {
                      string message = exception.Message;
                      RasterException rasterException = RasterException.FromHResult(exception.HResult);
                      if (rasterException != null)
                         message = rasterException.Message;
                      System.Diagnostics.Debug.WriteLine(message);
                   }
                }
                
                
                // Saving
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                savePicker.DefaultFileExtension = ".jpg";
                savePicker.FileTypeChoices.Add("JPEG Files", new List<string>() { ".jpg" });
                StorageFile file = await savePicker.PickSaveFileAsync();
                if (file != null)
                {
                   try
                   {
                      ILeadStream leadStream = LeadStreamFactory.Create(file);
                      using (IDisposable disposable = leadStream as IDisposable)
                      {
                         using (RasterCodecs codecs = new RasterCodecs())
                         {
                            await codecs.SaveAsync(image, leadStream, RasterImageFormat.Jpeg, 0);
                         }
                      }
                   }
                   catch (Exception exception)
                   {
                      string message = exception.Message;
                      RasterException rasterException = RasterException.FromHResult(exception.HResult);
                      if (rasterException != null)
                         message = rasterException.Message;
                         System.Diagnostics.Debug.WriteLine(message);
                      }
                   }
                                  
    

  2. Using WinRT (Windows Store) locally packaged files:

    [C#]

                // Loading
                Uri assetUri = new Uri("ms-appx:///Assets/Asset.jpg");
                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(assetUri);
                if (null != file)
                {
                   try
                   {
                      //Open the file as a WinRT RandomAccessStream and create an ILeadStream from this RandomAccessStream
                      ILeadStream leadStream = LeadStreamFactory.Create(file);
                      using (IDisposable disposable = leadStream as IDisposable)
                      {
                         using (RasterCodecs codecs = new RasterCodecs())
                         {
                            _viewer.Image = await codecs.LoadAsync(leadStream);
                         }
                      }
                   }
                   catch (Exception exception)
                   {
                      string message = exception.Message;
                      RasterException rasterException = RasterException.FromHResult(exception.HResult);
                      if (rasterException != null)
                         message = rasterException.Message;
                         System.Diagnostics.Debug.WriteLine(message);
                      }
                   }
                                  
    
  3. Using WinRT (Windows Store) application local folder:

    [C#]

                                        
                
                // Loading
                StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                StorageFile file = await localFolder.GetFileAsync("sample.jpg");
                if (null != file)
                {
                   try
                   {
                      //Open the file as a WinRT RandomAccessStream and create an ILeadStream from this RandomAccessStream
                      ILeadStream leadStream = LeadStreamFactory.Create(file);
                      using (IDisposable disposable = leadStream as IDisposable)
                      {
                         using (RasterCodecs codecs = new RasterCodecs())
                         {
                            _viewer.Image = await codecs.LoadAsync(leadStream);
                         }
                      }
                   }
                   catch (Exception exception)
                   {
                      string message = exception.Message;
                      RasterException rasterException = RasterException.FromHResult(exception.HResult);
                      if (rasterException != null)
                         message = rasterException.Message;
                         System.Diagnostics.Debug.WriteLine(message);
                   }
                }
                
                // Saving
                StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                StorageFile file = await localFolder.CreateFileAsync("sample.jpg");
                if (null != file)
                {
                   try
                   {
                      ILeadStream leadStream = LeadStreamFactory.Create(file);
                      using (IDisposable disposable = leadStream as IDisposable)
                      {
                         using (RasterCodecs codecs = new RasterCodecs())
                         {
                            await codecs.SaveAsync(image, leadStream, RasterImageFormat.Jpeg, 0);
                         }
                      }
                   }
                   catch (Exception exception)
                   {
                      string message = exception.Message;
                      RasterException rasterException = RasterException.FromHResult(exception.HResult);
                      if (rasterException != null)
                         message = rasterException.Message;
                         System.Diagnostics.Debug.WriteLine(message);
                   }
                }
                                     
    
  4. Using the HttpClient Class to download images:

    [C#]

                                      
                
                HttpClientHandler handler = new HttpClientHandler();
                HttpClient httpClient = new HttpClient(handler);
                HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://www.leadtools.com/images/page_graphics/leadlogo2.png"));
                         
                Stream stream = await response.Content.ReadAsStreamAsync();
                try
                {
                   //Open the file as a WinRT RandomAccessStream and create an ILeadStream from this RandomAccessStream
                   IInputStream inputStream = stream.AsInputStream();
                   ILeadStream leadStream = LeadStreamFactory.Create(inputStream, true);
                   using (IDisposable disposable = leadStream as IDisposable)
                   {
                      using (RasterCodecs codecs = new RasterCodecs())
                      {
                         RasterImage image = await codecs.LoadAsync(leadStream);
                         System.Diagnostics.Debug.WriteLine(image.Width.ToString());
                         _viewer.Image = image;
                      }
                   }
                }
                catch (Exception exception)
                {
                   string message = exception.Message;
                   RasterException rasterException = RasterException.FromHResult(exception.HResult);
                   if (rasterException != null)
                      message = rasterException.Message;
                      System.Diagnostics.Debug.WriteLine(message);
                }
                                   
    

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.