Blog posts


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

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