Blog posts about image library


  • Convert to BitmapImage

    March 30, 2011 | Tags: WPF, Image Library, Open source, Snippet
    Want to convert your KalikoImage (if your using C# Image Library) into a System.Windows.Media.Imaging.BitmapImage for use with for instance WPF, then here's your code (kalikoImage being your KalikoImage object):
    MemoryStream ms = new MemoryStream();
    kalikoImage.SaveBmp(ms);
    ms.Position = 0;
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = ms;
    bi.EndInit();

    If you instead want to convert a System.Drawing.Bitmap to a System.Windows.Media.Imaging.BitmapImage, then this is your code (myBmp being your Bitmap object):
    MemoryStream ms = new MemoryStream();
    myBmp.Save(ms, ImageFormat.Bmp);
    ms.Position = 0;
    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = ms;
    bi.EndInit();

    Once you have your BitmapImage, you can pass that into a System.Windows.Controls.Image:
    System.Windows.Controls.Image img = new System.Windows.Controls.Image();
    img.Source = bi;

  • C# Image Library version 1.2.1 released

    April 29, 2010 | Tags: Image Library, C#
    New minor maintenance release with cleaned up code (much thanks to ReSharper :))

    Downloads:
    C# Image Library 1.2.1 - source code
    C# Image Library 1.2.1 - DLL binary

  • Watermarking the easy way

    March 01, 2010 | Tags: Morgan, Image Library, C#, Snippet
    This post will show how easy it is to do a bit of watermarking using the open source C# Image Library.

    What we'll do
    Watermarks can appear in many different ways. This sample will show how to create the following in just a couple of lines:

    (Image above courtesy of Morgan over at Imaginations. Check out his site for some beautiful CG imagery.)

    Step by step

    Preparations
    First of all, head over to http://code.google.com/p/imagelibrary/ and download the latest DLL (or source code if you want to compile it yourself).

    Secondly, you'll need a watermark to apply to your images. Create a PNG image with very transparent layers in both black and white. The one I've created has black texts in a layer of 10% opacity as well as a white outline also with 10% opacity. (Please note that the watermark will be tiled, in other words used x * y times to cover the image you want to mark.)
    This is my watermark.png (applied on a gray background):


    Finally lets do some coding!
     Create a new project and add the C# Image library  you just downloaded (either the DLL or the project). Enter the following lines (in the appropriate place, depending of what type of project you're developing).
    using Kaliko.ImageLibrary;

    //...

    // Open the image you want to watermark
    KalikoImage image = new KalikoImage("c:\\MyImage.jpg");

    // Apply your watermark PNG image
    image.BlitFill("c:\\Watermark.png");

    // Save new marked image as high-quality jpeg
    image.SaveJPG("c:\\Marked.jpg", 99);

    That wasn't so hard, was it? :) The C# Image library is developed to ease common graphical tasks as this.

    Bookmark and Share

  • .Net unsharp mask filter

    January 20, 2010 | Tags: Photoshop, Image Library, C#, Snippet, Open source

    If you're familiar with Photoshop or any other photo manipulation tools you've probably come across a filter called unsharp mask. It works by making a gaussian blur copy of the image and then comparing it to the original image and extruding the differences, which appears around the edges, resulting in a very sharp and crisp image.

    This kind of filter is truely awesome if you're creating thumbnails as they useally becomes somewhat blurry. Applying an unsharp mask makes a very nice thumbnail.

    And if you're looking to do this with C# code, you've come to the right place. Because this filter was introduced in version 1.1 of the .Net Image Library.



    As with most of the filters I've tried to keep as close to the Photoshop filter parameters as possible. So if you're working with an Art Director that wants an unsharp mask with the amount of 32% and a radius of 1.4 pixels, you can just answer "No problem" and add one more line of code to your project.

    How to implement

    So lets start with the code. But first, head over to C# Image Library and download either the latest source code package or binary version or install through nuget.org.

    Add a reference to Kaliko.ImageLibrary.dll or the NuGet package ImageLibrary to your the source code project.

    To make this more readable, I'm assuming that you're also have these two lines in your code:

    using Kaliko.ImageLibrary;
    using Kaliko.ImageLibrary.Filters;

    We're assuming the above, that you want the equality to an amount of 32% and a radius of 1.4 pixels in Photoshop. The first parameter passed to UnsharpMaskFilter is the radius, the second the amount.

    // Load image from file
    KalikoImage image = new KalikoImage("c:\\images\\garden.jpg");
    
    // Create a thumbnail of 128x128 pixels
    KalikoImage thumb = image.GetThumbnailImage(128, 128, ThumbnailMethod.Crop);
    
    // Apply unsharpen filter (radius = 1.4, amount = 32%, threshold = 0)
    thumb.ApplyFilter(new UnsharpMaskFilter(1.4f, 0.32f, 0));
    
    // Save the thumbnail as JPG in quality 99 (very high)
    thumb.SaveJpg("c:\\images\\garden-thumbnail.jpg", 99);

    That was pretty easy, wasn't it? :)

    Use the faster unsharp mask filter

    The core .Net Image Library was written to be compatible with medium trust environments (such as many web hosting providers). But if you can run in a full trust web environment or are developing a Windows application you should definitely check out the package of optimized filters and use them instead. On a Intel i7-2600k the fast filter is about 5 times faster as the compatible filter!

    Use the faster filters by adding the ImageLibrary.FastFilters NuGet package to your project (make sure to select pre-release in the NuGet manager to get the latest version). All optimized filters are prefixed with Fast. So instead of UnsharpMaskFilter you use FastUnsharpMaskFilter.

    using Kaliko.ImageLibrary;
    using Kaliko.ImageLibrary.FastFilters;
    
    // ...
    
    // Load image from file
    KalikoImage image = new KalikoImage("c:\\images\\garden.jpg");
    
    // Create a thumbnail of 128x128 pixels
    KalikoImage thumb = image.GetThumbnailImage(128, 128, ThumbnailMethod.Crop);
    
    // Apply unsharpen filter (radius = 1.4, amount = 32%, threshold = 0)
    thumb.ApplyFilter(new FastUnsharpMaskFilter(1.4f, 0.32f, 0));
    
    // Save the thumbnail as JPG in quality 99 (very high)
    thumb.SaveJpg("c:\\images\\garden-thumbnail.jpg", 99);

    .NET Image Library also includes easy-to-use filters for:

    • Gaussian blur
    • Brightness
    • Contrast
    • Desaturation
    • Invert
    • Chroma key (blue/green screening)

    All these filters are also available as optimized multi-threaded versions.


  • C# Image Library version 1.2 released

    January 20, 2010 | Tags: Image Library, Open source, C#
    New version of the C# Image Library released. Introducing easy-to-use filters to the powerful features in the initial release.

    Downloads:
    C# Image Library 1.1 - source code
    C# Image Library 1.1 - DLL binary

    To get a quick start on how to apply the filters, please check this unsharp mask tutorial.

    Bookmark and Share