Take the following steps to start a project and to add some code that will demonstrate loading and saving images in WinRT using RasterCodecs.
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:
Click the Add button and then click OK to add the above references.
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>
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;
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"
};
Update the MainPage() constructor as shown below:
public MainPage()
{
this.InitializeComponent();
RasterSupport.Initialize();
}
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);
}
}
Build, and Run the program to test it.
Products |
Support |
Feedback: Loading and Saving Images in WinRT (Windows Store) |
Introduction |
Help Version 19.0.2017.6.21
|
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
Your email has been sent to support! Someone should be in touch! If your matter is urgent please come back into chat.
Chat Hours:
Monday - Friday, 8:30am to 6pm ET
Thank you for your feedback!
Please fill out the form again to start a new chat.
All agents are currently offline.
Chat Hours:
Monday - Friday
8:30AM - 6PM EST
To contact us please fill out this form and we will contact you via email.