Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction | Help Version 19.0.7.12
|
Take the following steps to start a project and to add some code that will demonstrate loading and saving images in WinRT using RasterCodecs.
<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>
using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Streams; using Windows.UI.Popups; using Leadtools; using Leadtools.Codecs;
private RasterImage _loadedImage; private string [] _extensions = new string [] { ".jpg", ".png", ".bmp", ".pcx", ".psd", ".cmp", ".tif", ".tiff", ".j2k", ".jbg", ".gif", ".jls", ".jb2", ".dcm", ".dic", ".pdf" };
public MainPage() { this.InitializeComponent(); RasterSupport.Initialize(); }
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); } }