Pages

Monday, June 27, 2011

Browser Performance Test

I heard a lot about browser war and performance challenges with each other.
Then got the chance to check HTML 5 Speed Benchmark with a random link from Internet Explorer Test Drive.

Here are the test results across different browsers.

Chrome 12: Google Chrome took 31.543 Seconds


Firefox 5: Mozilla Firefox took 15.067 seconds


Safari  5.0.5: Apple Safari is not up to the HTML5 benchmark L


 IE 9: Microsoft IE 9 took 9.405 seconds


Finally, IE 10 (Platform Preview 2.10.1000.16394) took just 8.223 seconds to sing ABC’s


Clearly, IE 10 wins the competetion with a great margin. However, if we compare the alphabet pronunciation while browser sings … Google Chrome will get the trophy :)

Monday, June 20, 2011

Generate Thumbnail Image

As part of our developer career we came across displaying user’s uploaded image onto the web page. This is the most common requirement when working for a photo manager site like Picasa or SnapFish or social networking domain.

Displaying image on a web page is not a big issue. However when considering the page performance we need to count if there are too many images within the page and most importantly the size of the image (good when in KB and worst in MB)

The worst scenario is when the end user uploads a photo which is of very high resolution say approximately ~2 - 4 MB in size.

To improve the page performance we either need to restrict user not to upload high resolution images or handle user’s images to render out thumbnail images which eventually occupy less weight on the page.

We have a technique in Asp.net to generate thumbnail images. We generally use "GetThumbnailImage" method of Bitmap class to generate thumbnail image of any size.

The following code snippet will generate thumbnail image as response header provided original image's binary stream and expecting thumbnail image width and height:

private void GetThumbnailImage(byte[] objByteStream, int intWidth, int intHeight)
{
byte[] image = (byte[])objByteStream;
System.Drawing.Bitmap uploadedimage = new System.Drawing.Bitmap(convertByteArrayToStream(image));
System.Drawing.Bitmap b = resizeImage(uploadedimage, intWidth, intHeight);
System.Drawing.Image.GetThumbnailImageAbort dummyCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image thumbNailImg = uploadedimage.GetThumbnailImage(b.Width, b.Height, dummyCallBack, IntPtr.Zero);
MemoryStream mstream = new MemoryStream();
thumbNailImg.Save(mstream, ImageFormat.Jpeg);
byte[] image2 = mstream.ToArray();
HttpContext.Current.Response.ContentType = "image/jpeg";
HttpContext.Current.Response.BinaryWrite(image2);
}

All we need is to create a handler class (.ashx) to render the newly generated thumnbail image back to the page.

Monday, May 16, 2011

Optimized way to check if a dataset has any value

Working with datasets and we should make sure that object contains some data within.
Here is the optimized way to check if a dataset has any data within by using advanced .Net concepts like extension methods and Linq.

public static bool IsEmpty(this DataSet dataSet)
{
return dataSet == null || !(from DataTable t in dataSet.Tables where t.Rows.Count > 0 select t).Any();
}

And we can use this extension method as follows:

DataSet dset = ... // Populate dataset by business method or logic
if (!dset.IsEmpty())
{
   ...
}

Extension methods provide a simple mechanism to extend types in the system (value, reference, and interface types) with new methods. These methods extend the original type and can be called like regular, defined instance methods, but they leave the original type and its methods untouched. Extension methods create the illusion that they are defined on a real type, but, in reality, no changes are made to the original types.

.NET Language-Integrated Query defines a set of general purpose standard query operators that allow traversal, filter, and projection operations to be expressed in a direct yet declarative way in any .NET-based programming language.

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

Error "unable to evaluate expression because the code is optimized or a native frame is on top of the call stack" appears within asp.net using C#.

I noticed this error whenever I try to redirect to a page from within try catch block.

To fix this issue we need to use some extra code (marked in red color) within Response.Redirect method:

Response.Redirect("~/NewPage.aspx" , false);

Specifying whether the current page execution will end immediately or not.

Friday, February 4, 2011

5 Types of Programmers

Five types of Programmers:
My fellow programmers, make your position clear after reading all the 5 points


If you find any other type do let me know.

Cheers