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.
Using the Microsoft File Picker Dialogs:
// 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);
}
}
Using WinRT (Windows Store) locally packaged files:
// 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);
}
}
Using WinRT (Windows Store) application local folder:
// 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);
}
}
Using the HttpClient Class to download images:
HttpClientHandler handler = new HttpClientHandler();
HttpClient httpClient = new HttpClient(handler);
HttpResponseMessage response = await httpClient.GetAsync(new Uri("https://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 |
Feedback: Working with image streams and files 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.