Blog posts


  • 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

  • Extract image tags from HTML in C#

    February 25, 2010 | Tags: C#, Regular expressions, Nils, Snippet
    A pretty little C# function that returns a list of strings containing all image tags extracted from a HTML string.
    private List<string> GetImagesInHTMLString(string htmlString) {
    List<string> images = new List<string>();
    string pattern = @"<(img)\b[^>]*>";

    Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
    MatchCollection matches = rgx.Matches(htmlString);

    for(int i=0, l=matches.Count; i<l; i++) {
    images.Add(matches[i].Value);
    }

    return images;
    }

  • Specify your canonical link

    February 23, 2010 | Tags: SEO, Google
    Playing around with Google's great Webmaster Tool I noticed that a site I'm working on had several URLs containing the same meta description. This was caused not by generic meta data, but rather that Google saw a page reposted with a querystring for toggling an archive tree as a seperate page. This makes two or more pages with identical content which isn't a good thing search engine-wise.

    Enter the canonical link
    You can tell Google which page that should be used when indexing. This is done by adding a link element to your page's header.
    <link rel="canonical" href="http://www.mysite.com/blog.aspx" />
    This would divert indexing of URLs like http://www.mysite.com/blog.aspx?archive=feb.

    Read a far better explanation at Google's blog, how to specify your canonical meta data.

  • Delay a call in javascript

    February 23, 2010 | Tags: Javascript, Snippet
    Here's a little snippet to add a little delay before calling a function. The delayTime is set in milliseconds (500 = half a second). This can be very useful for hovering menus etc.
    <script type="text/javascript">

    var timeOut;
    var delayTime = 500;

    function delayedCall(t) {
    timeOut = setTimeout(function() { alert(t); }, delayTime);
    }

    function removeDelayedCalls() {
    try {
    clearTimeout(timeOut);
    }
    catch(ex) { }
    }

    </script>

    You would most likely change the function call in delayedCall, how cool an alert ever can be ;). Also you might want to expand the parameter lists. I've added one just to show how they could be passed down to your final function.

    Here is a sample implementation on a HTML anchor element, when hovered waits 0.5 seconds before showing the alert. If the user during this time removes the mouse pointer from the link it cancel the delayed call by calling the removeDelayedCalls function:
    <a onmouseover="delayedCall('Hello world');" onmouseout="removeDelayedCalls();">Say hello</a>

  • Exporting SQL Server data to script

    January 29, 2010 | Tags: Database, SQL
    There are some situation where data in one SQL server database need to be exported as a script in order to publish it to another server.

    There are plenty of scripts and tools around the web, for instance Red Gate's SQL Data Compare tool is pretty nice. But if you're on a tight budget, this one might be out of reach. So what to do?

    If you're running SQL Server 2008 you already have this feature. You can use the generate script wizard in order to get the data as INSERT-statements.

    If you're running SQL Server 2000/2005 you need to download Microsoft SQL Server Database Publishing Wizard. It's a stand alone tool that lets you script the database structure and/or the data contained in your database.

    Note on the database publishing wizard for 2000/2005: You might get a 'file already in use' when you export. If so, just delete the file, back one step and have another go. It usually works.