Is this blog dead?

You might have noticed the lack of new posts at this blog in the last few months, but don’t worry, I’ll be back 🙂

As it happens, I’m currently on “paternity leave” (if that’s the correct English term). I’m at home taking care of my wonderful daughter Julia who is now 14 months old.

I’ll be back at work in September and hopefully I’ll have new things to write about at that time.

Have a good time till then!

/Emil

Extracting the base character from a character marked with a diacritic

Ever wondered how you can translate European national characters with diacritics such as å, ä, ö, ó etc into their base characters (a and o)? This can be useful for example when constructing filenames from user-given strings.

This is one way to do it:

string s = "ö";
string normalizedString = s.Normalize(NormalizationForm.FormD);

Console.WriteLine("Composite character: " + s[0]);
if (normalizedString.Length > 1)
{
   Console.WriteLine("Base character: " + normalizedString[0]);
   Console.WriteLine("Diacritic character: " + normalizedString[1]);
}

The result will be:

Composite character: ö
Base character: o
Diacritic character: ¨

Obviously, the key is the String.Normalize function. When we pass it NormalizationForm.FormD as the requested Unicode normalization form, it will separate all composite characters into their constituents. If the char is not a composite, then nothing will happen to it.

Note that the resulting string will be longer that the original if characters were separated. If needed it’s easy to iterate over the characters to filter our non-letters using conditions such as

if (CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.LowercaseLetter ||
    CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.UppercaseLetter)
{
   ...
}

Happy character-mangling!

Removing haze from images with Unsharp Mask

This is a handy tip for removing (or lessening) haze from an image using the unsharp mask filter found in most image editing programs. Normally that filter is used for sharpening an image, but it has more uses…

Here’s an image of the Chrysler Building in New York (taken from the Empire State Building if you’re curious). It was a hazy day when I took the picture so the image low contrast:

Chrysler Building - before unsharp mask

This haze can be fixed using the Unsharp mask filter. The dialog looks like this in Photoshop Elements 5:

Unsharp Mask dialog in Photoshop Elements 5

I use the following values:

  1. Amount: 40
  2. Radius: 150
  3. Threshold: 0

The resulting image is much better:

Chrysler Building - after unsharp mask

As hazy images are almost inevitable when shooting outside with long distances, I thought this might make a good tip. BTW, I first saw it in a Swedish Photo mag called “DigitalFoto för alla”.

/Emil

Blog back online again…

You may have noticed that the blog has been unavailable a few days. As you’re reading this, it’s obviously back online now again 🙂

I actually have no idea what happened. My web hosting service provider claims to not have done anything, and I have not messed with anything either. Strange…

If you’re reading this and have the same problem, it might be useful to know what I did to bring the blog back from the dead:

  1. Changed the site to use PHP 5 instead of PHP 4. Did not help, but I left the new setting.
  2. Upgraded WordPress to version 2.3.1. This did solve the problem!

Let’s hope things will work now. This is actually the first breakdown I’ve had. So far I’m satified with WordPress, it does what it does well, with no fuss.

/Emil

Signalling events from a class

For easy reference, here’s how to create an event in a class that listeners can react to:

class DeleteSubOrderEventArgs : EventArgs
{
    public int SubOrderId;
    public DeleteSubOrderEventArgs(int subOrderId)
    {
        SubOrderId = subOrderId;
    }
}

class Foobar
{
    public delegate void DeleteEventDelegate(object sender, DeleteSubOrderEventArgs e);
    public event DeleteEventDelegate DeleteSubOrderClick;
    
    protected void DeleteButton_Click(object sender, ImageClickEventArgs e)
    {
        // Signal to listeners
        if (DeleteSubOrderClick != null)
        {
            DeleteSubOrderClick(this, new DeleteSubOrderEventArgs(SubOrder.SubOrderId));
        }
    }
}

How to embed an Xslt-file in an assembly

Problem
How do I embed an Xslt file into an assembly so that I won’t have to deploy the file together with the assembly, set configuration options to refer to the file, etc?

Solution

  1. Create a resource (.resx) file in the project
  2. In the resource designer, click “Add Resource” and choose “Add Existing File…”. Select the Xslt file.
  3. Give the new resource a describing name, such as “FilterContentXslt”. The contents of the Xslt file will be available in a string property with this name in the Resource manager.
  4. Code that performs the transformation:
    // Parse the content into an XmlDocument
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xmlValue);
    
    // Retrieve the embedded resource containing the XSLT transform
    XmlDocument xsltDoc = new XmlDocument();
    xsltDoc.LoadXml(Resources.FilterContentXslt);
    
    XslCompiledTransform trans = new XslCompiledTransform();
    trans.Load(xsltDoc);
    
    // Perform the transformation
    StringWriter writer = new StringWriter();
    trans.Transform(doc, writer);
    string newXmlValue = writer.ToString();
    

Simple, and it works.

/Emil

Databinding with a GridView

Here’s how to call a codebehind function for the rows of a GridView. No rocket science, but useful for reference…

ASPX:
< %# GetStatusImage(Container) %>

Code behind:
protected string GetStatusImage(IDataItemContainer row)
{
}

or

ASPX:
< %# GetStatusImage(Container as GridViewRow) %>

Code behind:
protected string GetStatusImage(GridViewRow row{
}

/Emil

Get the Url to the EPiServer start page incl. language branch

This is how to retrieve the address to the startpage including the currently active language branch (e.g. “/sv/” or “/en/”):

PageData pagedata = Global.EPDataFactory.GetPage(Global.EPConfig.StartPage);
string url = pagedata.DetermineAutomaticURL(LanguageContext.Current.CurrentLanguageBranch);

This code looks simple but it actually took me an hour or so to get it right (I haven’t worked with globalization before) so I thought it’d make a good blog post…

/Emil