Loading and Saving Images in WinRT (Windows Store)

Take the following steps to start a project and to add some code that will demonstrate loading and saving images in WinRT using RasterCodecs.

  1. Start Visual Studio 2012
  2. Choose File --> New Project from the menu.
  3. In the New Project dialog box, choose Visual C# and Windows Store as the project template, and choose Blank App (XAML) in the Templates List.
  4. Type the project name as WinRT LoadSave in the Project Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK.
  5. In the Solution Explorer window, right-click on the References folder for the project and select Add Reference... from the context menu. Browse to the <LEADTOOLS_INSTALLDIR>\Bin\WinRT8_1\ folder (depending on your target platform), and select the following .WINMD files:

    • Leadtools.winmd
    • Leadtools.Kernel.winmd
    • Leadtools.Codecs.winmd
    • Leadtools.Codecs.Kernel.winmd
    • Leadtools.Codecs.Bmp.winmd
    • Leadtools.Codecs.Cmp.winmd
    • Leadtools.Codecs.Fax.winmd
    • Leadtools.Codecs.Gif.winmd
    • Leadtools.Codecs.J2k.winmd
    • Leadtools.Codecs.Jb2.winmd
    • Leadtools.Codecs.Jbg.winmd
    • Leadtools.Codecs.Jls.winmd
    • Leadtools.Codecs.Jxr.winmd
    • Leadtools.Codecs.Pcx.winmd
    • Leadtools.Codecs.Png.winmd
    • Leadtools.Codecs.Psd.winmd
    • Leadtools.Codecs.Tif.winmd
    • Leadtools.ColorConversion.winmd
    • Leadtools.Dicom.winmd
    • Leadtools.Dicom.Constants.winmd
    • Leadtools.ImageProcessing.Color.winmd
    • Leadtools.ImageProcessing.Core
    • Leadtools.ImageProcessing.Effects.winmd
    • Leadtools.ImageProcessing.Utilities
    • Leadtools.Pdf.winmd

    Click the Add button and then click OK to add the above references.

  6. Open the default.html file and copy the below html code into the editor:

    <Page   
      x:Class="WinRT_LoadSave.MainPage"  
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
      xmlns:local="using:WinRT_LoadSave"  
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
      mc:Ignorable="d">  
                         
      <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" HorizontalAlignment="Center" VerticalAlignment="Center" >  
        <Grid.RowDefinitions>  
           <RowDefinitionHeight="100" />  
           <RowDefinition Height="100" />  
        </Grid.RowDefinitions>  
        <Buttonx:Name="_loadButton"Grid.Row="0" Content="Load Image" Click="LoadButton_Click" />  
        <Buttonx:Name="_saveButton" Grid.Row="1" Content="Save Image" Click="SaveButton_Click" />   
     </Grid>  
                     </Page> 

  7. Switch to the MainPage.xaml code view (right-click MainPage.xaml in the Solution Explorer and select View Code) and add the following lines at the beginning of the file:

    using  Windows.Storage; 
    using  Windows.Storage.Pickers; 
    using Windows.Storage.Streams; 
    using  Windows.UI.Popups; 
    using  Leadtools; 
    using  Leadtools.Codecs; 

  8. Switch to MainPage.xaml code view (right-click MainPage.xaml in the Solution Explorer then select View Code) and add the following class level variables:

    private  RasterImage _loadedImage; 
                     private string [] _extensions = new string [] 
                     { 
      ".jpg", ".png",   
      ".bmp", ".pcx",    
      ".psd", ".cmp",    
      ".tif", ".tiff",    
      ".j2k", ".jbg",    
      ".gif", ".jls",    
      ".jb2", ".dcm",   
      ".dic", ".pdf"   
                     }; 

  9. Update the MainPage() constructor as shown below:

    public  MainPage() 
    { 
       this.InitializeComponent(); 
       RasterSupport.Initialize(); 
    } 

  10. Add the following class functions:

    private async void  ShowMessage(string  message) 
                     { 
      MessageDialog msgDlg = new  MessageDialog(message); 
      await msgDlg.ShowAsync(); 
                     } 
                     
                     private async void LoadButton_Click(object sender, RoutedEventArgs e) 
                     { 
      FileOpenPicker openPicker = new  FileOpenPicker(); 
      openPicker.ViewMode = PickerViewMode.Thumbnail; 
      openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
                     
      foreach (String extension in _extensions) 
         openPicker.FileTypeFilter.Add(extension); 
                     
      StorageFile file = await  openPicker.PickSingleFileAsync(); 
      if  (null  != file) 
      { 
         RasterCodecs codecs = new  RasterCodecs(); 
         //Open the file as a WinRT RandomAccessStream  
         try 
         { 
            IRandomAccessStream stream = await  file.OpenAsync(FileAccessMode.Read); 
            ILeadStream leadStream = LeadStreamFactory.Create(stream, true); 
            _loadedImage = await  codecs.LoadAsync(leadStream); 
            ShowMessage("Image loaded successfully"); 
         } 
         catch (Exception ex) 
         { 
            String errorMessage = null; 
            RasterException rasterException = RasterException.FromHResult(ex.HResult); 
            if (rasterException != null) 
            { 
               if (rasterException.Message != null) 
                  errorMessage = rasterException.Message; 
               else 
                  errorMessage = string.Format("Code: {0}", rasterException.Code); 
            } 
            else 
               errorMessage = ex.Message; 
                     
            ShowMessage(errorMessage); 
         } 
      } 
                     } 
                     
                     private async void SaveButton_Click(object sender, RoutedEventArgs e) 
                     { 
      if (_loadedImage == null) 
      { 
         ShowMessage("Must load an image first!"); 
         return; 
      } 
                     
      FileSavePicker savePicker = new  FileSavePicker(); 
      savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; 
      savePicker.FileTypeChoices.Add(".jpeg", new  List<string>{".jpg"}); 
      savePicker.DefaultFileExtension = ".jpeg"; 
      try 
      { 
         StorageFile file = await savePicker.PickSaveFileAsync(); 
         if (file != null) 
         { 
            IRandomAccessStream stream = await  file.OpenAsync(FileAccessMode.ReadWrite); 
            ILeadStream leadStream = LeadStreamFactory.Create(stream, true); 
            using (IDisposable disposable = leadStream as  IDisposable) 
            { 
               //Save the image  
               RasterCodecs codecs = new  RasterCodecs(); 
               await codecs.SaveAsync(_loadedImage,  //RasterImage to save  
                                      leadStream,    //ILeadStream to save into  
                                      RasterImageFormat.Tif, //The RasterImageFormat which tells our RasterCodecs object which codec to use.   
                                      0);   //specifying 0 Bits Per Pixel tells tells the RasterCodecs to use the default Bits Per Pixel for the specified format.   
                     
               ShowMessage("Image saved successfully"); 
            } 
         } 
      } 
      catch (Exception ex) 
      { 
         String errorMessage = null; 
         RasterException rasterException = RasterException.FromHResult(ex.HResult); 
         if (rasterException != null) 
         { 
            if (rasterException.Message != null) 
               errorMessage = rasterException.Message; 
            else 
               errorMessage = string.Format("Code: {0}", rasterException.Code); 
         } 
         else 
            errorMessage = ex.Message; 
            ShowMessage(errorMessage); 
      } 
                     } 

  11. Build, and Run the program to test it.

  12. Click the Open File button, and select an image.
  13. NOTE: If you encounter an "Invalid File Format" or "Feature Not Supported" exception, please refer to the topic Invalid File Format/Feature Not Supported.
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document
Click or drag to resize