Blog posts


  • 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

  • Error #2130 Unable to flush sharedObject

    November 20, 2009 | Tags: Actionscript, AS3, Troubleshooting, Flash

    This annoying error was shown recently in a Flash project I was working on. At first I thought it was a sandbox problem, but Googling around didn’t help. Some links said it was a Flash 10 problem, but I had been succesfully used almost the exact same code in another project.

    This is a shortened version of the code:

    var name:String = "/demosite/";
    var path:String = "/";
    var paramname:String = "test";
    var value:String = "myValue";

    // Open SharedObject
    var _so:SharedObject = SharedObject.getLocal(name, path);

    // Create object
    var o = new Object();
    o.value = value;
    o.timestamp = new Date();

    _so.data[paramname] = o;
    _so.flush(); // Flush to disc

    _so.close();

    Can you spot what I did wrong? If not, I’ll tell you. It’s the name that causes the “Error #2130 Unable to flush sharedObject” message. Or more exact, the “/” in the name. Replacing it with:

    var name:String = "demosite";

    ..solved the problem. I really like good descriptions in error messages ;)