Main goal: asynchronous loading, modifying and saving images from any thread (out of UI thread for WPF/WP8).

This PCL library can be used in WP8 / WPF / ASP.NET 4.5 and .NET 4.5 applications.

How to use:

Installation:

PM> Install-Package PortableImagingLibrary

WP8 Example:

/// Loading PNG image from WP8 resource

var image = new PngImage();

var uri = new Uri("Assets/Tiles/logo.png", UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(uri);
await image.LoadAsync(sri.Stream);

/// Modifying

int x = 1;
int y = 1;

uint colorARGB = image.GetPixel(x,y); // Get color of the specified point.
colorARGB = colorARGB / 2; // Modifying color value.
image.SetPixel(x, y, colorARGB); // Set color for specified pixel.

/// Saving PNG image into isolated storage

var fileName = "Shared/ShellContent/newLogo.png";
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
    using (var pngSteam = new IsolatedStorageFileStream(fileName, FileMode.Create, isoStore))
        await image.SaveAsync(pngSteam);
    }
}

.NET Example:

/// Loading PNG image

var image = new Image();
using (var pngStream = new FileStream(pngFile, FileMode.Open, FileAccess.Read)) {
    await image.LoadPngAsync(pngStream);
}

/// Modifying

int x = 1;
int y = 1;

uint colorARGB = image.GetPixel(x,y); // Get color of the specified point.
colorARGB = colorARGB / 2; // Modifying color value.
image.SetPixel(x, y, colorARGB); // Set color for specified pixel.

/// Saving PNG image

using (var pngSteam = new FileStream(pngFile, FileMode.Create, FileAccess.Write)) {
    await image.SavePngAsync(pngSteam, new PngSaveOptions() {
        BitDepth = 8, 
        ColorType = ColorType.TruecolorWithAlpha 
    });
}

Current limitations:

  • Only PNG image format supported;
  • Grayscale PNG not supported;
  • Saving images as RGB (true color) and ARGB (true color with alpha) PNG only.

Last edited Jul 24, 2013 at 9:15 AM by AndreyDotNet, version 7