Wednesday, February 16, 2011

GIMP Easy Icon Creation

It's not often I need to make icons but it happens sometimes. Having no favorite tool for creation I tried GIMP. I was surprised how easy it can create icon with several images.

First of all create image with biggest size you need. I chose 64x64. You might also want to create a layer with background to see the result better. I usually use white or dark-blue depending on foreground image lightness.

Then duplicate the layer with icon and select "Scale Layer.." from layer's right-click menu. Resize it to next size you need, ex. 32x32. Note, that unlike in Photoshop you can have layers with different size. By default you'll see current layer's border.

Let's create the last layer 16x16 and colorize layers with different colors. Also delete background before saving icon (leave only layers with icon images).

Now save document as .ico file.

Now you have icon with all the images. On the following screenshot explorer shows different images in file list window (16x16 icon) and preview window (smaller sized 64x64 icon).

Saturday, February 5, 2011

HtmlAgilityPack and XPath Peculiarity

Parsing several HTML pages I noticed that HtmlAgilityPack doesn't consider that its node has relative path for XPath. The following code illustrates this:

var html = @"
<div class="
"header"">
    <p>header <span>paragraph 1-1</span></p>
    <p>header <span>paragraph 1-2</span></p>
</div>
<div class="
"content"">
    <p>content <span>paragraph 2-1</span></p>
    <p>content <span>paragraph 2-2</span></p>
<div>
    "
;

var doc = new HtmlDocument();
doc.LoadHtml(html);

var node = doc.DocumentNode.SelectSingleNode("div[1]/p[1]");

Console.WriteLine("\r\n1st <p> in 1st <div>:");
Console.WriteLine(node.OuterHtml);

Console.WriteLine("\r\nCount of <span> (//):");
Console.WriteLine(node.SelectNodes("//span").Count);

Console.WriteLine("\r\nCount of <span> (.//):");
Console.WriteLine(node.SelectNodes(".//span").Count);

It produces the output:

1st <p> in 1st <div>:
<p>header <span>paragraph 1-1</span></p>

Count of <span> (//):
4

Count of <span> (.//):
1

w3schools.com says that "//" selects nodes "from the current node". So does it mean that HtmlAgilityPack works wrong?

Learning XPath on w3schools.com I had no doubt. But W3C specification says that it's alright:

//para selects all the para descendants of the document root and thus selects all para elements in the same document as the context node
.//para selects the para element descendants of the context node

All I wanna say is that you must be cautious to the information you got, even if it from the popular site with a good reputation (like w3schools is). "Trust no one", like Horde says :).