Sunday, December 20, 2009

Compare Google, Bing and Yahoo webmaster tools – who indexes faster?


Well, we all know that these giants provide search-engines which make our life better, by pointing us to the right web pages that we’re searching for. They have their own crawlers to find out what all of these web pages has as contents and to index (assuming that you know what is indexing) them. To assist this process, they all have webmaster tools too, so that web site owners can submit and update the information related to the websites they own to these search engines using sitemaps. The following list shows their webmaster tools and the links to them, as currently available.

Google Google Webmaster Tools http://www.google.com/webmasters/
Bing Webmaster Center http://www.bing.com/webmaster
Yahoo! Site Explorer http://siteexplorer.search.yahoo.com/

Following is my experience with the above tools and the inference based on them, which might be totally different from yours and can change from time to time.

The First Experience:
    For one of my website, a standard sitemap was created and was submitted to all of these webmaster tools almost at the same time. To be precise, Sitemap was submitted to Google and Bing on the same day and was submitted to Yahoo few days later.

Result:
    Google indexed the pages first which happened with few days and started showing them in their search results. Bing took slightly more than a week to index them while Yahoo totally ignored them.

The second experience:
    After around 2 months, the site had to be moved to another domain, which means another URL. The same content had to be placed in the new links. For the sake of indexing, the old domain was kept alive with only the updated sitemap (removed old links) and the content was moved to the new website with a sitemap of its own. Again, both the website’s sitemap was submitted to these tools at the same time.

Result:
    Here again, Google found my new website within a week and started showing them as indexed. It took slightly more than a week for Google to remove my old website links from their list. For Bing, even after two months, my old website links are still showing up in results and nothing from my new website is indexed. Yahoo Site Explorer is the simple one here, since nothing from my websites has been indexed in their list until now.

Conclusion:
    May be, there are technicalities involved based on moving the same content among websites OR the dependency of various aspects that controls the indexing process for search engines is too complex to accommodate the above scenario OR the other tools does well in other scenarios. But, whatever that is, for a user, for a specific content, based on the previous and the experiences stated above, it is found Google comes up with result of your sitemap submission first regardless of whether that is positive or negative. For a moment, I thought that we are going to have multiple options for web searches. But, it looks like I will have to stick with Google for updated results.

Friday, December 18, 2009

Get and Set values for all form elements using JavaScript


Here, we will cover the basic logic to retrieve and set values for various HTML form elements using a sample JavaScript code.

This discussion assumes that you are familiar with the methods like document.getElementById, document.getElementsByName etc.. to find and load the element for further processing. For a quick note, if it is assumed that you have a textbox with id, ‘myText’, then that text can be accessed by document.getElementsById(‘myText’).

The logic is pretty simple and is based on the concept that each element has it’s own individual property to be set. Below is the table that shows just that.

Element Type Property to be used Comments
checkbox checked  
hidden value  
radio checked  
text value  
textarea value  
select-one selected
OR
selectedIndex
This is a dropdown. It can be either set by setting the ‘selected’ property of the options collection OR setting the selectedIndex property of the dropdown itself.
select-multiple selected
OR
selectedIndex
This is multi-select dropdown which is created using multiple="multiple" for a select tag. Works similar to select-one.

To get the value of a particular element, use it’s appropriate property based on the type of the element being processed. The below code shows how that can be done in a simple manner. Note that the select-multiple returns an array of selected values. This code is presented in simple if..else syntax for simplicity. Provide error catching mechanism as necessary.

function getValue(element)//returns the value
{
if (element==null) return null;
var returnValue;
if (element.type=="select-one")
{
//dropdown (select-one)
returnValue = element.options[element.selectedIndex].value;
}
else if (element.type=="select-multiple")
{
//multi-select drop down
var returnArray = new Array();
for(var i = 0; i < element.options.length; i++)
{
if(element.options[i].selected == true)
{
returnArray.push(element.options[i].value);
}
}
return returnArray;
}
else if (element.type=="checkbox")
{
//checkbox element
returnValue = Boolean(element.checked);
}
else if (element.type=="radio")
{
//radio element
returnValue = Boolean(element.checked);
}
else
{
//text, textarea, hidden
returnValue = element.value;
}
return returnValue;
}

To set the value, it’s just the reverse process, which is shown below.

function setValue(element, value)//return whether the set was successful.
{
if (element==null) return false;
if (value==null) return false;
var returnValue;
if (element.type=="select-one")
{
//dropdown (select-one)
for(var i = 0; i < element.options.length; i++)
{
if(element.options[i].value == value)
{
element.selectedIndex = i;
returnValue = true;
break;
}
}
}
else if (element.type=="select-multiple")
{
//multi-select drop down, expects the value to be an array of selected values
for(var j = 0; j < value.length; j++)
{
for(var i = 0; i < element.options.length; i++)
{
if(element.options[i].value == value[j])
{
element.options[i].selected = true;
break;
}
}
}
returnValue = true;
}
else if (element.type=="checkbox")
{
//checkbox element
element.checked = Boolean(value);
returnValue = true;
}
else if (element.type=="radio")
{
//radio element
element.checked = Boolean(value);
returnValue = true;
}
else
{
//text, textarea, hidden
element.value = value;
returnValue = true;
}
return returnValue;
}

Additional thoughts:
If, the controls are rendered from an ASP.net application, there may be times where the value from a Label control is to be set or retrieved. Since a Label is rendered as a SPAN element, this can be done by enhancing the code for checking the nodeName OR tagName (or any appropriate property) and using innerHTML to set or get the value of the SPAN element.


Friday, December 11, 2009

Simple and advanced methods for creating thumbnail images in .net using c#


When I stumbled upon GetThumbnailImage method available in the Image class for the first time, I immediately went ahead with the thought that now I have a native and efficient way to create thumbnails. I used a code similar to what shown below.

Option 1: using GetThumbnailImage

private Image getThumbNailUsingGetThumbnailImage(string fileName)
{
Image img = Image.FromFile(fileName);
return img.GetThumbnailImage(300, 300, null, IntPtr.Zero);
}

The result was quick. But, soon it was found that this will not work as efficiently as I expected. The biggest problem was that when the image got edited by any of the image editing tools out there, the thumbnail creation went wrong. It was because of the reason that GetThumbnailImage was depending up on the image metadata as it was set when the image got created. If the image thumbnail property is not getting modified by your image editing tool, GetThumbnailImage will fail to get the right thumbnail. Below is a similar method that retrieves the thumbnail from the PropertyItem collection of an image.

Option 2: Using PropertyItem

private Image createThumbFromProperty(string file)
{
Image image = new Bitmap(file);
Image Thumb = null;
PropertyItem[] propItems = image.PropertyItems;
foreach (PropertyItem propItem in propItems)
{
if (propItem.Id == 0x501B)
{
byte[] imageBytes = propItem.Value;
MemoryStream stream = new MemoryStream(imageBytes.Length);
stream.Write(imageBytes, 0, imageBytes.Length);
Thumb = Image.FromStream(stream);
break;
}
}
return Thumb;
}

Thus started a lookout for a performance-improved simple way to achieve this which resulted in the following methods. Basically, these methods used GDI+. The logic seems pretty simple, which is to create another small in-memory based on the size of the thumbnail needed. It breaks down to this.

1. Load the original image
2. Get the proportional size of the image based on the original Image size and target thumbnail size
3. Redraw the image to the new canvas

A sample code for this is shown below

Option 3: Using GDI+ for simple thumbnail
private Image getThumbNail(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
Image im = Image.FromStream(fs);
Size szMax = new Size(300, 300);
Size sz = getProportionalSize(szMax, im.Size);
// superior image quality
Bitmap bmpResized = new Bitmap(sz.Width, sz.Height);
using (Graphics g = Graphics.FromImage(bmpResized))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(
im,
new Rectangle(Point.Empty, sz),
new Rectangle(Point.Empty, im.Size),
GraphicsUnit.Pixel);
}
im.Dispose(); im = null;
fs.Close(); fs.Dispose(); fs = null;
return bmpResized;
}

private Size getProportionalSize(Size szMax, Size szReal)
{
int nWidth;
int nHeight;
double sMaxRatio;
double sRealRatio;

if (szMax.Width < 1 || szMax.Height < 1 || szReal.Width < 1 || szReal.Height < 1)
return Size.Empty;

sMaxRatio = (double)szMax.Width / (double)szMax.Height;
sRealRatio = (double)szReal.Width / (double)szReal.Height;

if (sMaxRatio < sRealRatio)
{
nWidth = Math.Min(szMax.Width, szReal.Width);
nHeight = (int)Math.Round(nWidth / sRealRatio);
}
else
{
nHeight = Math.Min(szMax.Height, szReal.Height);
nWidth = (int)Math.Round(nHeight * sRealRatio);
}

return new Size(nWidth, nHeight);
}

Now that we know how to draw things on the Bitmap (canvas), we can play around and create framed thumbnails as shown below. The code below will create a frame effect around the thumbnail.

Option 4: Using GDI+ for framed thumbnail
private Image getThumbNailWithFrame(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
Image im = Image.FromStream(fs);
Size szMax = new Size(300, 300);
Size sz = getProportionalSize(szMax, im.Size);
// superior image quality
Bitmap bmpResized = new Bitmap(sz.Width, sz.Height);
using (Graphics g = Graphics.FromImage(bmpResized))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, sz.Width, sz.Height);
int FrameWidth = 5;//decides the frame border width
g.DrawRectangle(new Pen(Color.Silver, FrameWidth - 2), 0, 0, sz.Width - 1, sz.Height - 1);
FrameWidth += 5;//decide the frame width
g.DrawImage(im, new Rectangle(FrameWidth, FrameWidth, sz.Width - FrameWidth * 2, sz.Height - FrameWidth * 2), new Rectangle(Point.Empty, im.Size), GraphicsUnit.Pixel);
}
im.Dispose(); im = null;
fs.Close(); fs.Dispose(); fs = null;
return bmpResized;
}

We can even extend this concept to create shades around the generated thumbnail. The below code demonstrates that on a white background.

Option 5: Using GDI+ for shaded thumbnail

private Image createThumbnailUsingGDI(ref Image imgPhoto, int destWidth, int destHeight)
{
int sourceX = 0;
int sourceY = 0;

int destX = 0;
int destY = 0;
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;

Bitmap b = new Bitmap(destWidth, destHeight);

Graphics grPhoto = Graphics.FromImage(b);

grPhoto.FillRectangle(Brushes.DarkGray, new Rectangle(destX, destY, destWidth, destHeight));
grPhoto.DrawLine(new Pen(Brushes.LightGray), new Point(0, destHeight - 1), new Point(destWidth, destHeight - 1));
grPhoto.DrawLine(new Pen(Brushes.LightGray), new Point(destWidth - 1, 0), new Point(destWidth - 1, destHeight));
//shade right
grPhoto.FillRectangle(Brushes.White, new Rectangle(destWidth - 3, 0, 7, 2));
grPhoto.FillRectangle(Brushes.White, new Rectangle(destWidth - 2, 0, 7, 4));
grPhoto.FillRectangle(Brushes.White, new Rectangle(destWidth - 1, 0, 7, 6));

//shade botton
grPhoto.FillRectangle(Brushes.White, new Rectangle(0, destHeight - 3, 2, 7));
grPhoto.FillRectangle(Brushes.White, new Rectangle(0, destHeight - 2, 4, 7));
grPhoto.FillRectangle(Brushes.White, new Rectangle(0, destHeight - 1, 6, 7));
grPhoto.DrawImage(imgPhoto, new Rectangle(destX + 2, destY + 2, destWidth - 7, destHeight - 7), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

grPhoto.Dispose();
return b;

}

These methods do provides some flexibility and they are efficient enough for small scale applications. But, if we're talking about large number of images where memory and performance are critical factors, these may not suffice. We can fine tune the above methods by any/all of the following approaches and of course, lot others.

1. Having proper "using" keywords
2. making sure that the objects are destroyed properly
3. Having the thumbnail creating method loaded as static so that is always ready for you.
4. Get yourself equipped with knowledge on Graphics class which has numerous drawing capabilities and various brushes.

Once you have your thumbnail generation code ready, use it with extensive multi-threading concepts to give the user a pleasant feeling. If you're looking for any basic threading knowledge, look at my other article here.

I know, there may be few who might be hoping that we could've had something simple like GetThumbnailImage which didn't had any pitfalls. Well, there may be. System.Windows.Media.Imaging might be the place to look at for these capabilities.

Please note that this article is for learning purposes. If you are here for quick code to be used for production purposes, please look elsewhere.

Reference:
http://danbystrom.se