Blog posts


  • .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

  • jQuery, select and options

    January 20, 2010 | Tags: Javascript, jQuery, Snippet
    Here's a couple of snippets that covers some aspects of working with jQuery to manipulate SELECT-elements.

    Add options to a SELECT-element with jQuery

    <select id="mySelect"></select>

    <script language="javascript" type="text/javascript">
    var tmp = new Array();

    tmp.push("<option value=\"my value\">option #1</option>");
    tmp.push("<option value=\"my value 2\">option #2</option>");

    $("#mySelect").html(tmp.join(''));

    </script>

    Getting the selected value

      $('#mySelect').val();
    If you have a multiple choice element you should go with this code:
      $('#mySelect option:selected').each(function(i, opt){  
    alert($(opt).val());
    });

    Getting the text of the selected option

      $('#mySelect option:selected').text();
    If you have a multiple choice element you should go with this code:
      $('#mySelect option:selected').each(function(i, opt){  
    alert($(opt).text());
    });

    Remove a specified option

    This piece of code removes the option that has value=1 from the selectbox.
      $('#mySelect option[value=1]').remove();

    Clear a selectbox

      $('#mySelect').html('');

  • Thumbnails the easy way

    December 14, 2009 | Tags: Image Library, ASP.NET, Open source, C#, Snippet

    This is the first code sample using the .NET Image Library released under open source license. It covers thumbnail creation, which can be a bit of a hassle some times.

    Image Library can handle thumbnail creation in three different ways: cropping, padding and fitting.

    To illustrate the different options, here's a couple of samples using a test image of 1024 x 768 pixels in a 256 x 256 pixels thumbnail.

    Thumbnail by cropping

    This option will ensure that the whole thumbnail area will contain graphics, even if that mean that some of the original image won't be visible.

    Thumbnail by padding

    This option first fit the whole image inside the dimension provided to the method. Then the thumbnail is padded with the background color to fill out the whole image.

    Thumbnail by fitting

    This is the more traditional version that you might have come across in the original System.Drawing.Image GetThumbnailImage method.
    It ensures that the complete original image is visible within the thumbnail. This may result in a ratio that is different from the dimensions defined in the function call.

    Okay, it's time for some coding :)

    First of all, get the Image Library here:

    .NET Image Library source code at GitHub

    or

    .NET Image Library package at NuGet

    Create your own project and reference the .NET Image Library NuGet-package, dll or project.

    The following code will create the three different types of thumbnails (saving them as jpeg in quality 99%).

    using Kaliko.ImageLibrary;

    namespace TestApp {
    public class ThumbnailTests {

    public static void TestCreatingThumbnails() {

    KalikoImage image = new KalikoImage("garden.jpg");

    // Create thumbnail by cropping
    KalikoImage thumb = image.Scale(new CropScaling(128, 128));
    thumb.SaveJPG("thumbnail-crop.jpg", 99);

    // Create thumbnail by fitting
    thumb = image.Scale(new FitScaling(128, 128));
    thumb.SaveJPG("thumbnail-fit.jpg", 99);

    // Create thumbnail by padding. Pad with Color.Wheat
    image.BackgroundColor = Color.Wheat;
    thumb = image.Scale(new PadScaling(128, 128));
    thumb.SaveJPG("thumbnail-pad.jpg", 99);
    }
    }
    }


  • Create object from a class string in .net

    December 09, 2009 | Tags: ASP.NET, C#, Snippet

    Have you ever wanted to create an object from a string describing what class you want to instantiate from? Perhaps not, but it might come in use if you for instance want to implement some kind of plug-in system for your application or maybe toggle between different data access classes from web.config.

    In this example I will show you how to create an interface that our different classes will implement. It’s necessary that they all follow the same interface when we later want to call a function in the object we’ve created.

    The interface

    namespace ClassFromStringTest {
    public interface ICommon {
    string WhoAmI();
    }
    }

    Okay, this is pretty straightforward. Each class will implement this interface with one single function (WhoAmI) that will return a string to help us identify which class that is instantiated.

    Class 1

    namespace ClassFromStringTest {
    class Class1 : ICommon {
    public string WhoAmI() {
    return "I'm Class1";
    }
    }
    }

    Class 2

    namespace ClassFromStringTest {
    class Class2 : ICommon {
    public string WhoAmI() {
    return "I'm Class2";
    }
    }
    }

    Class 3 – External!

    Now we’ll do something different to really show the power of this. We’re creating a class in a new project and assembly!

    Be sure to include your first project in the new one in order to be able to use the ICommon interface.

    namespace ExternalNewClass {
    public class MyOtherClass : ClassFromStringTest.ICommon {
    public string WhoAmI() {
    return "I'm an external library";
    }
    }
    }

    Testing the classes

    It’s time to switch back to the first project and add a class for testing. Add a reference to your ExternalNewClass project so that you can instantiate the MyOtherClass as well.

    using System;

    namespace ClassFromStringTest {
    class ClassTester {
    public static string WhoAreYou() {
    string myClassName = "ClassFromStringTest.Class1";
    string retval = "";

    try {
    ICommon myClass = (ICommon)Activator.CreateInstance(Type.GetType(myClassName));
    retval = myClass.WhoAmI();
    }
    catch (Exception ex) {
    retval = "Error: " + ex.Message;
    }
    return retval;
    }
    }
    }

    You can change myClassName to any of:

    • ClassFromStringTest.Class1
    • ClassFromStringTest.Class2
    • ExternalNewClass.MyOtherClass

    The magic in this is the System.Activator.CreateInstance function that creates an object from the class name you feed it with. Using Type.GetType(string) instead of the other variants of CreateInstance is that it can handle different assemblies much easier. Notice how we add the assembly name before the class name, for instance ClassFromStringTest.Class1. The returning object is then casted to our interface ICommon so that we know that we can reach it’s methods.

    Hope you’ll find this useful :)


    Bookmark and Share