Monday, May 30, 2011

Image in C#: Save, Resize, and Convert to Binary

Sooner or later, you will find the need of handling image in your code, especially when you're working with a database. So in this article, I share with you some useful methods that I find myself using frequently. They include: saving image in a folder, getting thumbnail from the original image, converting image to binary for saving in database, and converting binary data back to image.

Save An Image

Image originalImage = Image.FromFile(imagePath);
string filePath = AppDomain.CurrentDomain.BaseDirectory + savedName;
originalImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);This code saves the image to the base directory of your app or website.
imagePath is the full path to the image that we need to save (e.g. "C:\Pictures\Lighthouse.jpg"), one way to get this is use an OpenFileDialog.
savedName is the name for the saved image.

Save the Image as a Thumbnail

public static void SaveImage(string imagePath, string savedName,
int width = 0, int height = 0)
{
Image originalImage = Image.FromFile(imagePath);
string filePath = AppDomain.CurrentDomain.BaseDirectory + savedName;

if (width > 0 && height > 0)
{
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback);
Image imageToSave = originalImage.GetThumbnailImage
(width, height, myCallback, IntPtr.Zero);
imageToSave.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
originalImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
private static bool ThumbnailCallback() { return false; }Note the parameters: int width = 0 & int height = 0. This is a C# 4.0 feature: Optional Parameters, so we can call this method like this: (assume this method is in the ImageHandling class).

// Save image as original
ImageHandling.SaveImage(@"C:\Pictures\Lighthouse.jpg", "OriginalLighthouse.jpg");
// Save image as thumbnail
ImageHandling.SaveImage(@"C:\Pictures\Lighthouse.jpg",
"ThumbnailLighthouse1.jpg", 160, 90);
// New feature: Named Parameter
ImageHandling.SaveImage(@"C:\Pictures\Lighthouse.jpg",
"ThumbnailLighthouse2.jpg", height: 90, width: 160);Resize the Image and Keep Aspect Ratio

int newWidth = originalImage.Width * percentage / 100;
int newHeight = originalImage.Height * percentage / 100;To keep the image aspect ratio, simply replace width & height parameters with percentage parameter and call the GetThumbnailImage method with our new width & height.

Convert Image to Binary

public static byte[] ImageToBinary(string imagePath)
{
FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length);
fileStream.Close();
return buffer;
}Use this method when you want to save images in your database. The column to store binary data is usually varbinary(MAX).

Convert Binary to Image
You store images in your database as binary, of course you must convert binary back to images so you can display them in your app.

public static Image BinaryToImage(System.Data.Linq.Binary binaryData)
{
if (binaryData == null) return null;

byte[] buffer = binaryData.ToArray();
MemoryStream memStream = new MemoryStream();
memStream.Write(buffer, 0, buffer.Length);
return Image.FromStream(memStream);
}You may need to add reference to System.Data.Linq. LINQ-to-SQL maps a varbinary column in your database to its relevant property as System.Data.Linq.Binary.

Sunday, May 29, 2011

Using Speech class in .net

Overview

Introduction
The Speech API library that we are going to use today is represented by the file sapi.dll which’s located in %windir%\System32\Speech\Common. This library is not part of the .NET BCL and it’s not even a .NET library, so we’ll use Interoperability to communicate with it (don’t worry, using Visual Studio it’s just a matter of adding a reference to the application.)

Implementation
In this example, we are going to create a Console application that reads text from the user and speaks it. To complete this example, follow these steps:

As an example, we’ll create a simple application that reads user inputs and speaks it. Follow these steps:

1.Create a new Console application.

2.Add a reference to the Microsoft Speech Object Library

3.Write the following code and run your application:


01.// C#
02.using SpeechLib;
03.static void Main()
04.{
05. Console.WriteLine("Enter the text to read:");
06. string txt = Console.ReadLine();
07. Speak(txt);
08.}
09.static void Speak(string text)
10.{
11. SpVoice voice = new SpVoiceClass();
12. voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
13.}


01.' VB.NET
02.Imports SpeechLib
03.Sub Main()
04. Console.WriteLine("Enter the text to read:")
05. Dim txt As String = Console.ReadLine()
06. Speak(txt)
07.End Sub
08.Sub Speak(ByVal text As String)
09. Dim voice As New SpVoiceClass()
10. voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault)
11.End Sub

If you are using Visual Studio 2010 and .NET 4.0 and the application failed to run because of Interop problems, try disabling Interop Type Embedding feature from the properties on the reference SpeechLib.dll.

Building Talking Strings
Next, we’ll make small modifications to the code above to provide an easy way to speak a given System.String. We’ll make use of the Extension Methods feature of LINQ to add the Speak() method created earlier to the System.String. Try the following code:

01.// C#
02.using SpeechLib;
03.static void Main()
04.{
05. Console.WriteLine("Enter the text to read:");
06. string txt = Console.ReadLine();
07. txt.Speak();
08.}
09.static void Speak(this string text)
10.{
11. SpVoice voice = new SpVoiceClass();
12. voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
13.}

01.' VB.NET
02.Imports SpeechLib
03.Imports System.Runtime.CompilerServices
04.Sub Main()
05. Console.WriteLine("Enter the text to read:")
06. Dim txt As String = Console.ReadLine()
07. txt.Speak()
08.End Sub
09. _
10.Sub Speak(ByVal text As String)
11. Dim voice As New SpVoiceClass()
12. voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault)
13.End Sub

I Love YOU ♥
Let’s make it more interesting. We are going to code a VBScript file that says “I Love YOU” when you call it. To complete this example, these steps:

1.Open Notepad.
2.Write the following code:

1.CreateObject("SAPI.SpVoice").Speak "I love YOU!"

Of course, CreateObject() is used to create a new instance of an object resides in a given library. SAPI is the name of the Speech API library stored in Windows Registry. SpVoice is the class name.

3.Save the file as ‘love.vbs’ (you can use any name you like, just preserve the vbs extension.)
4.Now open the file and listen, who is telling that he loves you!
Microsoft Speech API has many voices; two of them are Microsoft Sam (male), the default for Windows XP and Windows 2000, and Microsoft Ann (female), the default for Windows Vista and Windows 7.

Generating email address images

Nowadays, you can't publish an email address anywhere without receiving a bunch of spam immediately after. Those spam web crawlers just search in every site looking for everything and anything that resembles an email address.

I very often see people use different variations of writing their email like "myname at mydomain.com" or "myname at mydomain dot com".
I don't think this covers it anymore...
The best way, in my opinion, is to create an image of the email address instead of writing it. (That's what Facebook does as well.)

I started working on a user based site, and the users' emails will be displayed throughout the site. So I created a class that will dynamically generate email address images for me so my precious users won't be so vulnerable to all the spam out there.

Here it is:

// Members //
private int _emailFontSize = 8;
private string _emailFontFamily = "Verdana";
private Brush _emailBackgroundColor = Brushes.White;
private Brush _emailFontColor = Brushes.Navy;

// Properties //
// I cut this out, just for convenience //

// Methods //
public void CreateEmailImage(string email)
{
// create the font object
Font myFont = new Font(_emailFontFamily, _emailFontSize);

// create the image object
Bitmap emailImage = new Bitmap((int)(myFont.SizeInPoints * email.Length),
myFont.Height);

Graphics imgGraphics = Graphics.FromImage((Image)emailImage);
imgGraphics.FillRectangle(_emailBackgroundColor,
new Rectangle(new Point(0, 0), emailImage.Size));
imgGraphics.DrawString(email, myFont, _emailFontColor, new PointF(0, 0));

// measure the actual size of the email string
SizeF stringSize = imgGraphics.MeasureString(email, myFont);

// crop the image we created
Bitmap finalImage = CropImage(emailImage,
new Rectangle(new Point(0, 0), new Size((int)stringSize.Width,
(int)stringSize.Height)));

// save the image to a local file
finalImage.Save(email + ".gif", ImageFormat.Gif);
}

private Bitmap CropImage(Image imgCrop, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap(cropArea.Width, cropArea.Height - 1);
Graphics bmpG = Graphics.FromImage(bmpImage);
bmpG.DrawImage(imgCrop, new Point(0, 0));
return bmpImage;
}
}

10 things to make your desktop database apps better

Data driven applications rely on their databases
Everyone knows that any app driven by data is much more than just the app. In most cases the app without a database doesn’t even function, or fails to function properly. If a database is an integral part of your application, then shouldn’t you be doing all you can to ensure it stays healthy and prepare for the worst case events of corruption or dead drives?

This week we have been contacted by 5 long time users who suddenly lost or corrupted their data. Two of these were end user data that is going to cost a huge amount of labor to reproduce. In all of these cases the following simple procedures would have prevented the situation entirely.


1 – Backup your databases
This seems pretty obvious, right? The database is just a file, xcopy it and make a backup. Enforce backups within your app, make it an integral part of the application. If a user accidentally deletes a huge part of their data, how can you recover it? With a backup it is pretty easy.

Example Strategy
When your app starts up make a backup of the database in a subfolder. A simple name of \backups within your application directory. If you want to get fancy you could even create a subdirectory under that for the YYYY-MM, or when you backup the database rename it DatabaseName-YYYY-MM.vdb4. Then you can easily show the user backups in the directory and let them pick one to restore.

To prevent the backups from growing without bounds, you can periodically sum up the sizes of them all and delete the oldest ones until you are under some threshold for size. I can almost guarantee you that having 100MB of disk space for backups is no big deal to most users these days. If that gives them a month of backups they will thank you immensely should something bad happen in the future.

2 – Ensure clean shutdown detection in single user apps
Have you ever had Microsoft Outlook crash on you while running? What happens when it restarts? Outlook will revalidate the PST file to ensure it is not corrupt.

Every app that has a database connection open should have some way to detect if it was shutdown clean. It is the only way you will know that the application was terminated (either by the user, a crash, or the OS).

Remember that VistaDB lives WITHIN your application. So when your app dies, so does the engine. A server system doesn’t have to worry about this issue because the server service is still the only thing ever talking to the file.

Example Strategy
I typically include at startup a check for a registry key. If the key still exists at startup then the app is either still running locally, or it was not shutdown clean during the last run. I put the clear function for the registry key as the very last thing that happens before the application exits. Not when I want to exit (you could still crash trying to shutdown cleanly), but right before the actual exit.

In a C# program you can do this in the Program Main function. Always put a try / catch block around the main running of your application, then add a finally that clears the shutdown.

If you want a great tool to help catch other application errors and report them I suggest Gibraltar Software. They have made it very easy to integrate a general purpose catch error system into almost any app.

3 – Ensure clean shutdown detection in multi user apps
Multi user apps are a little harder to track because you may not be the only one in the database at the time an app fails or is terminated (or the OS crashes, etc). You should still follow the above for detecting your actual application shutting down correctly, but you now have to add some more complex logic around the packing / cleanup case.

Example Strategy
We have deployed a fairly simple strategy by adding a System_Settings table on some projects. This settings table would include only 1 row normally, but include information about the last time a backup was made (to prevent every app from trying to backup when they startup), and whether the database had a dirty shutdown. When the dirty flag is set (from shared mode) the application then tries to open the database exclusive to pack. Depending upon your application you could be checking for this periodically within your app from other desktops and if the flag is seen release all connections for some period of time and then start trying to access the database again.

There are lots of different ways you could do this depending upon your application design. You could also put a flat file next to the database dirty.txt that every connection class checks before opening the database, etc. The important thing is to make sure that dirty shutdowns are detected and can be recovered without user intervention.

4 – Handle exceptions in your code
See the Gibraltar Software page for software to help you do this everywhere in your code. But even if you don’t use something like their software, you still need to ensure everywhere something can go wrong you handle it.

What if you run out of disk space during an insert? What if updating a row throws a concurrency exception? Ensure you handle those cases.

Example Strategy
The easiest way to do this is to encapsulate your database functions into a Data Access Layer (DAL). It does not have to be a separate assembly outside your application. Just ensure that you have a uniform way of accessing the database and can trap all errors. Log them to ensure that errors you didn’t expect can easily be reported back to you, even if the user has to go find a flat file called “Errors.txt” in the directory and email it to you, that is better than nothing.

5 – Put all objects in using() statements
So many of the Microsoft examples should show this best practice, but don’t. I personally don’t think it adds anything to the readability of the sample, and it is a best practice. So it should be shown at all times.

Basically anytime you are allocating an object that has a disposing on it you should be putting it into a using() block. This ensures that the GC is going to clean it up as fast as possible. Calling .Close() on the object and setting it to nothing or null is NOT the same thing. You have to call .Close(), .Dispose() and then make sure all references are gone.

Example Strategy
using (VistaDB.Provider.VistaDBDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// Do something
}
}This one thing will do more to reduce your memory problems, fix phantom errors that occur, and so much more. I have fixed so many customer problems by simply adding using() around their object allocations. VistaDB is 100% managed code, if you don’t clean us up, or hold references to us, the objects can be pinned in RAM a very long time.

6 – Use transactions only when required
Transactions are expensive, very expensive for a file based database. Don’t use them to update a single entry in a single table, or create a transaction just to run a select query. Only use them when they make sense.

Also read the Transactions can hurt performance on our blog.

7 - Encrypt only when required, do not use for password control
This one comes into play quite often from former Access programmers. Access had this concept of a password for control of opening the file by the end user in their own copy of Access. There is no need for such a concept with VistaDB. In VistaDB 4 we changed the name from password to Encryption Phrase to make it clearer that the password is being used for encryption.

Encryption takes that base phrase, adds a salt to it, and then every page is encrypted at write time, and decrypted at load time. This is quite expensive (30% or more overhead), and if your database becomes corrupt we have no way to recover encrypted data (would sort of defeat the purpose of encryption).

You can turn on encryption for the database, and then turn it off for certain tables if you only need a single table encrypted. But this still leaves all the headers on disk encrypted, and cannot be recovered.

8 – Debug with a clean copy
Don’t debug against a production database (ever!). Especially running tests like NUnit you always want to start with a known state. The easiest way to do this is to put a pre-populated database in the application directory, and set the option in Visual Studio to Copy it to the output directory always. That way at each build you have a clean database in your debugging directory.

This is probably the #1 way people corrupt their databases. Stopping and killing the engine while doing lots of activity is eventually going to cause a corruption. Try to take SQL Server and kill it every time you stop debugging, I guarantee that you will end up with a bad database after enough iterations.

9 – Ensure regular maintenance of databases
Create a regular maintenance cycle within your app. The app could do this weekly, monthly, or based upon some internal defined “usage” case. If you know each run of your app can generate 10% turn over of data, then you should pack every 10 runs to keep the database at it’s smallest size and optimal format.

Example Strategy
Add a SYSTEM_MAINTENANCE table to your database that only has one row. LASTPACKED DateTime. Just that one field will help you determine when the database was last cleaned up. You could allow the user to do it, or prompt them, or do it in the background if you know you are at a low point for user activity.

The import thing is to have a regular plan for maintaining the database. SQL Server has these maintenance plans built in to perform index rebuilds, etc at periodic intervals. We don’t have that ability because we are only running when you are running, and we never know how long that run will last.

10 – Prepare for regular updates to components
Have a way to deploy updates to your users. You will eventually run into a bug that all your users need to avoid, or updated components from a vendor (like us). If you have no way to get that update out, you are going to have some unhappy users.

VistaDB updates periodically, and I am sure your other vendors do as well. We don’t put out updates for no reason. There are almost always fixes and updates in every build. You don’t have to rush to deploy a new major or minor, but builds within your major.minor should be considered required maintenance updates.

Sometimes customers contact us with problems from VistaDB 3.0 builds that they have never updated. Sorry, we publish regular updates, put them on the site, list them in RSS feeds, blog posts, etc. There are plenty of ways to see and get the updates. If you are running VistaDB 3.5 and are still using the initial builds you are doing yourself a disservice by not getting the more recent builds.

Any vendor product you are using that has not been updated in 12+ months is either dead (no new development), obsolete, or very trivial in nature.

Example Strategy
Pick a date (Microsoft uses the first Tuesday of the month) to go visit all your vendors. See if they have updates, read through the release notes. Plan to integrate and test them with your next build. If any of the updates from vendors are critical, then you have a critical update to perform as well.

Make sure you have a way to get updates to users. You don’t have to write all this yourself, Tarma Installer provides a very simple way to get web updates into your applications without writing complicated setup and download scripts.

Get File name from URL Path - ASP Classic

The following VBScript function can be used in an ASP legacy application to extract the file name from a request.

public function GetFileName()
dim files, url, segments

'get then current url from the server variables
url = Request.ServerVariables("path_info")

segments = split(url,"/")

'read the last segment
url = segments(ubound(segments))
GetFileName = url
end function

store custom objects in web.config

In this Post, I am going to discuss about web.config. Normally in our daily life, we used to have some data in appSettings section of web.config and read it when required. That is in string form. But there are lot more than this. We can update the data in web.config programmatically as well .

Now another main point is, we can store some object of custom type in web.config as well, which we normally don’t do it. But this can be very useful in several scenarios.

Have anyone tried to update some value or add some value in web.config? W’ll have brief discussion on this.

First, This is very common to have some constant data at appSettings section of web.config and read it whenever required. So how to read this ( for beginners).

//The data is stored in web.config as




// To read it
string message = ConfigurationManager.AppSettings["WelcomeMessage"];
Now if we want to update some data of appSettings programatically. One can do like this.

//Update header at config
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
config.AppSettings.Settings["WelcomeMessage"].Value = "Hello All, Welcome to my updated site.";
config.Save();
Now what do you do, if you want to add some data in appSettings . You can add some app.config data as below.

//Update header at config
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
config.AppSettings.Settings.Add("ErrorMessage", "An error has been occured during processing this request.");
config.Save();
The above code is adding one new key value pair in web.config file. Now this can be read anywhere in the application.

Now, the question is, Can we store some custom data at config?

Yes…

We can store some object. Let’s see how

I have created a sample example. In this example, I have saved an object of my custom class NewError in web.config file. And also updating it whenever required.

To do this, Follow the below steps.

a) Create a Class that inherit From ConfigurationSection (It is available under namespace System.Configuration ). Every property must have an attribute ConfigurationProperty, having attribute name and some more parameters. This name is directly mapped to web.config. Let’s see the NewError class

public class NewError:ConfigurationSection
{
[ConfigurationProperty ("Id",IsRequired = true)]
public string ErrorId {
get { return (string)this["Id"]; }
set { this["Id"] = value; }
}
[ConfigurationProperty("Message", IsRequired = false)]
public string Message {
get { return (string)this["Message"]; }
set { this["Message"] = value; }
}
[ConfigurationProperty("RedirectURL", IsRequired = false)]
public string RedirectionPage
{
get { return (string)this["RedirectURL"]; }
set { this["RedirectURL"] = value; }
}
[ConfigurationProperty("MailId", IsRequired = false)]
public string EmailId
{
get { return (string)this["MailId"]; }
set { this["MailId"] = value; }
}
[ConfigurationProperty("DateAdded", IsRequired = false)]
public DateTime DateAdded
{
get { return (DateTime)this["DateAdded"]; }
set { this["DateAdded"] = value; }
}
}
as you can see every property has attribute ConfigurationProperty with some value. As you can see the property ErrorId has attribute

[ConfigurationProperty ("Id",IsRequired = true)]it means ErrorId will be saved as Id in web.config file and it is required value. There are more elements in this attribute that you can set based on your requirement.
Now if you’ll see the property closely, it is bit different.

public string ErrorId {
get { return (string)this["Id"]; }
set { this["Id"] = value; }
}Here the value is saved as the key “id”, that is mapped with web.config file.

b) Now you are required to add/register a section in the section group to tell the web.config that you are going to have this kind of data. This must be in and will be as

allowDefinition="Everywhere"/>
c) Now one can add that object in your config file directly as


d) And to read it at your page. Read it as follows.

NewError objNewError = (NewError)ConfigurationManager.GetSection("errorList");
And also a new object can be saved programmatically as

NewError objNewError = new NewError()
{
RedirectionPage="www.rediff.com",
Message = "New Message",
ErrorId="0",
DateAdded= DateTime.Now.Date
};
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

config.Sections.Add("errorList", objNewError);
config.Save();
Even one can add a custom group and have some custom elements in in this section.

ASP.NET provides very powerfull APIs to read/edit the web.config file easily.

Resizing an Image On-The-Fly using .NET

The other day, I was given the requirement to be able to dynamically resize a JPEG image server-side before rendering it down to the browser, thus reducing unnecessary bandwidth usage between the server and the client.

Clearly, it would be more efficient to store the original image in the different sizes required, as resizing the images on-the-fly would put an unnecessary additional load on the server, however in this case, it wasn't an option.

Firstly, we are going to need the System.Drawing and System.Drawing.Drawing2D namespaces:

// C#
using System.Drawing;
using System.Drawing.Drawing2D;
' Visual Basic
Imports System.Drawing
Imports System.Drawing.Drawing2DSecondly, the signature for our method which is going to do the resizing:

// C#
public static Image ResizeImage(Image image, Size size, bool preserveAspectRatio = true)
{

}'
Visual Basic
Public Shared Function ResizeImage(ByVal image As Image, _
ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image

End FunctionAs you can see, the method takes two mandatory parameters: the image to be resized and the new size it is to be resized to. An optional third parameter specifies whether to preserve the image's original aspect ratio.

If we are not going to preserve the aspect ratio of the original image, then we simply set the height and width of the new image accordingly. However, if we do wish to preserve the aspect ratio, then the situation is a little more complex: Firstly, we need to calculate the percentage difference, in each axis (height and width), between the original image and the desired size. Then we use whichever difference is the smaller to calculate the new height and width of the new image:

// C#
int newWidth;
int newHeight;
if (preserveAspectRatio)
{
int originalWidth = image.Width;
int originalHeight = image.Height;
float percentWidth = (float)size.Width / (float)originalWidth;
float percentHeight = (float)size.Height / (float)originalHeight;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
newWidth = (int)(originalWidth * percent);
newHeight = (int)(originalHeight * percent);
}
else
{
newWidth = size.Width;
newHeight = size.Height;
}
' Visual Basic
Dim newWidth As Integer
Dim newHeight As Integer
If preserveAspectRatio Then
Dim originalWidth As Integer = image.Width
Dim originalHeight As Integer = image.Height
Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
Dim percent As Single = If(percentHeight < percentWidth, percentHeight, percentWidth)
newWidth = CInt(originalWidth * percent)
newHeight = CInt(originalHeight * percent)
Else
newWidth = size.Width
newHeight = size.Height
End IfNext, we create a blank bitmap using our new dimensions:

// C#
Image newImage = new Bitmap(newWidth, newHeight);
' Visual Basic
Dim newImage As Image = New Bitmap(newWidth, newHeight)Finally, we use the graphics handle of the new bitmap to draw the original image onto our new bitmap and return it:

// C#
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
' Visual Basic
Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
End Using
Return newImageYou can experiment with the interpolation mode to vary the quality of the resized image. Personally, I found HighQualityBicubic to give the best results. The code for the complete method is as follows:

// C#
public static Image ResizeImage(Image image, Size size,
bool preserveAspectRatio = true)
{
int newWidth;
int newHeight;
if (preserveAspectRatio)
{
int originalWidth = image.Width;
int originalHeight = image.Height;
float percentWidth = (float)size.Width / (float)originalWidth;
float percentHeight = (float)size.Height / (float)originalHeight;
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
newWidth = (int)(originalWidth * percent);
newHeight = (int)(originalHeight * percent);
}
else
{
newWidth = size.Width;
newHeight = size.Height;
}
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}
' Visual Basic
Public Shared Function ResizeImage(ByVal image As Image, _
ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
Dim newWidth As Integer
Dim newHeight As Integer
If preserveAspectRatio Then
Dim originalWidth As Integer = image.Width
Dim originalHeight As Integer = image.Height
Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
Dim percent As Single = If(percentHeight < percentWidth,
percentHeight, percentWidth)
newWidth = CInt(originalWidth * percent)
newHeight = CInt(originalHeight * percent)
Else
newWidth = size.Width
newHeight = size.Height
End If
Dim newImage As Image = New Bitmap(newWidth, newHeight)
Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
End Using
Return newImage
End FunctionWe can then call the method at an appropriate point to resize the image. In this example, I simply read the original image from disk and then save the resized image to a MemoryStream for use later in the application:

// C#
Image original = Image.FromFile(@"C:\path\to\some.jpg");
Image resized = ResizeImage(original, new Size(1024, 768));
MemoryStream memStream = new MemoryStream();
resized.Save(memStream, ImageFormat.Jpeg);
' Visual Basic
Dim original As Image = Image.FromFile("C:\path\to\some.jpg")
Dim resized As Image = ResizeImage(original, New Size(1024, 768))
Dim memStream As MemoryStream = New MemoryStream()
resized.Save(memStream, ImageFormat.Jpeg)

Using a Background Process in WPF

A simple trick for avoiding unresponsiveness of user interfaces in WPF


WPF applications often need to call time consuming methods or processes, and the time consuming methods or processes can be huge time consuming calculations or perhaps a Web Service call. In the case of WPF, specially XBAP (WPF browser application) which runs on a remote client machine's browser, this sort of calls can make your user interface unresponsive. I have working on WPF for more than four months. A lot of things have come my way and been dealt with nicely. Well in this post, I am going to share a simple trick for avoiding unresponsiveness of user interfaces. We have already used this technique in many cases in our application, but this is for WPF.

The "Thread & Invoke"
The BackgroundWorker is a very smooth and useful tool for our purpose of making more responsive UIs (user interfaces). Before discussing more about the BackgroundWorker, let's take a flash back of the legacy technique (which is pretty smart) of making more responsive UIs. Previously, I used threading for implementing such kinds of UI smoothness. What I did is create a background thread and call the expansive operation on that thread. When the job is finished, I use the MethodInvoker method to let know the UI thread that the job is finished. And this model is called the asynchronous model. And this is quite a smart model and the rest of the models are based on this approach. Here is a quick code snippet for demonstrating the technique.

Collapse | Copy Code
//first start the method with tread
System.Threading.ThreadStart ts =
new System.Threading.ThreadStart(ExpansiveMethod);
System.Threading.Thread t = new System.Threading.Thread(ts);
t.Start();
protected void ExpansiveMethod()
{
//Very expansive call will go here...
//after the job is finished call method to update ui
MethodInvoker updaterMI = new MethodInvoker(UpdateChange);
this.BeginInvoke(UpdateChange);
}
protected void UpdateChange()
{
//again back to main ui thread
}The "Background Worker"
Okay, it's time to use the BackgroundWorker. An amazing thing about the BackgroundWorker is, it's simple to use. First, let's see what a background worker is. BackgroundWorker is a class under System.ComponentModel which executes an operation on a separate thread. I was introduced in .NET Framework 2.0. Things are pretty simple, just like thread. All you have to do is instantiate a BackgroundWorker and subscribe to its events, and call the RunWorkerAsync() method. Let's see a code snippet. Since we are programmers, we understand code better.

Collapse | Copy Code
void MyMethodToCallExpansiveOperation()
{
//Call method to show wait screen
BackgroundWorker workertranaction = new BackgroundWorker();
workertranaction.DoWork +=
new DoWorkEventHandler(workertranaction_DoWork);
workertranaction.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(workertranaction_RunWorkerCompleted);
workertranaction.RunWorkerAsync();
}
void workertranaction_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Call method to hide wait screen
}
void workertranaction_DoWork(object sender, DoWorkEventArgs e)
{
//My Expansive call will go here...
}As you can see, in the above code, I have subscribed to two events: DoWork (which is the main function) and RunWorkerCompleted. In the DoWork event handler, we will put our expansive time consuming operations; as the name implies, the RunWorkerCompleted event is fired when the work is finished. BackgroundWorker also has a ProgressChanged event which is used to let the main UI thread know how much work is completed.

The "Dispatcher"
In a few cases, the BackgroundWorker needs to access the main UI thread. In WPF, we can use Dispatcher which is a class of System.Windows.Threading and a delegate to access the main thread. First of all, we have to declare a delegate for our candidate methods, and then use the delegate to call the method using Dispatcher. Dispatcher has different thread priorities and you can choose a priority from the DispatcherPriority enum. Send has the highest priority in DispatcherPriority.

Collapse | Copy Code
//delegate for our method of type void
public delegate void Process();
//and then use the dispatcher to call the method.
Process del = new Process(UpdateMyUI);
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, del);
void UpdateMyUI()
{
//get back to main UI thread
}

Hosting WCF Service

WCF service can be hosted in four ways :

1. Internet Information Server (IIS)

WCF services can be hosted within Internet Information Services. There is no requirement to write hosting code as part of the application and IIS automatically activates the service code as required. Services also benefit from IIS features such as management of process lifetime and automatic application restart after configuration changes. Services can be run within IIS by creating a .svc file, which contains the service code, and a configuration file, which contains binding information, and then saving them in an IIS virtual directory.

2. Windows Activation Service (WAS)

Windows Activation Service is the new process activation mechanism that is a feature of IIS 7.0. WAS builds on the existing IIS 6.0 process and hosting models, but is no longer dependent on HTTP. Although IIS 7.0 uses WAS over HTTP, WCF can use WAS to provide message-based activation over other protocols, such as TCP and named pipes. This helps WCF applications to take advantage of WAS features, such as process recycling, rapid fail protection, and the common configuration system, which were previously available only to HTTP-based applications.

3. Self-hosting

WCF services can be hosted inside any .NET managed application, such as console applications and Windows Forms or Windows Presentation Foundation (WPF) graphical applications. A developer creates a class that implements a WCF service contract interface, and specifies binding information in the application configuration file. The application code can then use an instance of System.ServiceModel.ServiceHost to make the service available at a particular Uniform Resource Identifier (baseAddress in the following code example).
[Visual Basic] Dim myHost As ServiceHost = New ServiceHost(GetType(MyService), baseAddress)
[C#] ServiceHost myHost = new ServiceHost(typeof(MyService), baseAddress)

4. Managed Windows Service

A WCF service can be registered as a Windows Service, so that it is under control of the Windows Service Control Manager (SCM). This is suitable for long-running WCF services that are hosted outside of IIS in a secure environment and are not message-activated. The WCF service benefits from the features of Windows Services, such as automatic start at start time and control by the SCM.
To host a WCF service in this way, the application must be written as a Managed Windows Service by inheriting from System.ServiceProcess.ServiceBase. It must also implement a WCF service contract interface and then create and open a ServiceHost to manage the WCF service.

How to store Database Connection string in web.config and get Connection sting in .cs file

You need to add connection string under AppSettings or Connection strings tag

Example 1 : In appSettings section in web.config

Web.Config





In .cs file , Configuration Manager class is used to fetch connection string . See below code to see how :

string strSQLConnection1 = ConfigurationManager.AppSettings["SQLConnection1"];

But the better way is to add connection string in connectionstrings tag in web.config

See Web.Config for details :





You can retrieve the connection string in web.config using Configuration Manager class in .cs file :

string strSQLConnection2 = ConfigurationManager.ConnectionStrings["SQLConnection2"].ConnectionString;

Saturday, May 21, 2011

Tips and Tricks about Google Talk

With Google Talk being all the craze right now, some people hating it, and others loving it, I figured that I would post a list of tips and tricks for those curious about the extra "features" Google implemented and has not said much about.

Registry Tweaks
You can edit most settings by opening regedit (start -> regedit),
and navigating to the key HKEY_CURRENT_USER\Software\Google\Google Talk.

The "Google/Google Talk" key has several sub-keys that hold different option values:

Accounts: This one has subkeys for each different account that has logged in on the client. These keys have different values that store the username, password and connection options.

Autoupdate: Stores the current version information. When the client checks for updates it compares Google's response with these values. If an update is needed, it will download and update the new version.

Options: This is the most interesting part, where most of the current hacks should be used (keep reading).

Process: Stores the process ID. Probably used by Google Talk to detect if it's already running or not.

1.) HKEY_CURRENT_USER\Software\Google\Google Talk\Options\show_pin
If 1, shows a "pin" next to the minimize button that keeps the windows on top of all the other open windows when clicked.

2.)HKEY_CURRENT_USER\Software\Google\Google Talk\Options\view_show_taskbutton

If 0, hides the taskbar button, and leaves the tray icon only, when the window is shown

3.)HKEY_CURRENT_USER\Software\Google\Google Talk\Options\away_inactive

If 1, status will be set as Away after the specified number of minutes.

4.)HKEY_CURRENT_USER\Software\Google\Google Talk\Options\away_screensaver

If 1, status will be set as Away after the specified number of minutes.

5.)HKEY_CURRENT_USER\Software\Google\Google Talk\Options\inactive_minutes
Number of inactive minutes to become away if auto-away is on.

Tips & Tricks
Wumpus Game : - First noted by a GoogleRumors commentor, if you add the buddy wumpus.game@gmail.com you can play the classic text-based game. Wumpus is an easter egg game that came with Google Talk (unfortunately, he didn't’t accept my invitation, so I can’t play).
Change the font size - While holding the control key, move the scroll wheel on your mouse either up or down. This trick works while being focused in either the read or write area.
Insert line breaks - If you want to have a message that spans multiple paragraphs, just hold shift and hit enter. You can add as many new lines as you want to create.
Bold Text - To write something bold, you can use an asterisk before and after the word, like *this* .
Italic Text - To use italics, use an underscore before an after the word, like _this_ .
Switch windows - Hitting tab will cycle through open windows. It will select minimized conversations, to expand them just hit enter. If you just want to cycle through IM's and don't care about the buddy list, control-tab will do that and will automatically expand a minimized conversation if you settle on one.
Invitation Tips - You don’t need to say Yes or No when someone wants to add you as a friend; you can simply ignore it, the request will go away. (On the other hand, someone with whom you chat often will automatically turn to be your friend, unless you disable this in the options).
Show Hyperlinks - You can show your homepage or blog URL simply by entering the it in your away message (at the top of the main window). It will automatically turn to a link visible to others.
Google Talk Game - “Google Talk” also was the name of a word game which uses Google.
A message can be 32767 characters long.
How To
Conference Calls :
What you need to do to have conference calls: Open up a copy of Google Talk on all computers with which you wish to conference. After one copy is opened make a new shortcut for Google Talk but at the end of it add /nomutex. If you installed it to the default folder then your shortcut should read "C:\Program Files\Google\Google Talk\googletalk.exe" /nomutex. Open 2nd instances of the software on every user's computer. After this start a chain: User 1 should connect on one instance to user 2. User 2 will connect on his second instance to user 3. User 3 will connect using his second instance back to user 1. With this chain everyone is connected to everyone.

Nickname & Status Message :
You can't change your nickname in a way that other people will see it change. Every nickname in the Google Talk contactlist is the part that is before @gmail.com (only the alphabetical characters are used) or the name you chosen for your GMail account. To change the nickname need to go to your Gmail account and change the name there. Choose Settings, Accounts, and then Edit info. Click on the second radio button, and enter your custom name. As a result all of your emails will have that nick as well, there is no way to seperate the two. You can add a website in your custom message, it will be clickable when someone opens a conversation window with you.

Contacts :
You don't need to say Yes or No when someone wants to add you as a friend; you can simply ignore it, the request will go away. (On the other hand, someone with whom you chat often will automatically turn to be your friend, unless you disable this).
The Gmail account 'user@gmail.com' can't be invited as your friend.

Sound & Video :
It's possible to broadcast music, MP3, etc.. through Google Talk.
Unplug your microphone. Double click on the speaker icon in the lower right corner. This will open up "Volume Control". Select "Options" and then "Properties". Then check the button next to "Recording" then click OK. You may also have to change your setting under Mixer Device. Now the Recording Control screen should be up. On my computer I selected "Wave Out Mix". Click on the green phone in Google Talk and call your friend.

Keyboard Shortcuts
Ctrl + E - It centralizes the selected text, or the current line.
Ctrl + R - It justifies to the right the selected text, or the current line.
Ctrl + L - It justifies to the left the selected text, or the current line.
Ctrl + I - The same thing does that Tab.
Tab - It is giving the area to each of the windows opened by Google Talk.
Ctrl + Tab - The same thing does that Shift + Tab .
Shift + Tab - The same thing does that Tab but in reverse.
Ctrl + Shift + L -Switch between points, numbers, letters, capital letters, roman numbers and capital roman numbers
Ctrl + 1 (KeyPad) - It does a simple space between the lines.
Ctrl + 2 (KeyPad) - It does a double space between the lines.
Ctrl + 5 (KeyPad) - A space does 1.5 between the lines.
Ctrl + 1 (NumPad) - It goes at the end of the last line.
Ctrl + 7 (NumPad) - It goes at the begin of the last line.
Ctrl + F4 - It closes the current window.
Alt + F4 - It closes the current window.
Alt + Esc - It Minimize all the windows.
Windows + ESC - Open Google Talk (if it's minimized, or in the tray)
F9 - Open Gmail to send an email to the current contact.
F11 - It initiates a telephonic call with your friend.
F12 - It cancels a telephonic call.
Esc - It closes the current window.
[HOWTO] Use multiple identities on Google Talk
Want to run Google Talk with multiple Gmail identities? If you have several Google Gmail accounts you also may want to run multiple instances of Google Talk This is especially important for families that share a single PC. Nothing worse than a family member signing you out so they can sign in under their own account!

Basically, to have "Google Polygamy" you need to run Google Talk with the following switch: /nomutex

Step 1: Right-click on the desktop
Step 2: Select New
Step 3: Select Shortcut
Step 4: Paste this into the text box:

"c:\program files\google\google talk\googletalk.exe" /nomutex

Step 5: Click Next and choose a shortcut name such as Google Talk1, Google Talk2, or something related to your Gmail account for easy remembering which account is which.
Step 6: Click OK a few times.
[HOWTO] Use Google Talk via a Web Browser
You want to use Google Talk anywhere ? Follow these guidelines :)

Step 1: Opens your favorite web browser at the following address :

http://www.webjabber.net:8080/jim/
Step 2: Follow the instructions of the Page.

Step 3: You can talk with your friends

Google's Secret Command-Line Parameters
There are a few secret parameters you can add to Google Talk and make it function differently.

The most important, I think, is /nomutex, which allows you to run more than one instance of GT. Here are the others:

/nomutex: allows you to open more than one instance of Google Talk
/autostart: when Google Talk is run with this parameter, it will check the registry settings to see if it needs to be started or not. If the "Start automatically with Windows" option is unchecked, it won't start.
/forcestart: same as /autostart, but forces it to start no matter what option was set.
/S upgrade: Used when upgrading Google Talk
/register: registers Google Talk in the registry, includig the GMail Compose method.
/checkupdate: check for newer versions
/plaintextauth: uses plain authentication mechanism instead then Google's GAIA mechanism. Used for testing the plain method on Google's servers.
/nogaiaauth: disables GAIA authentication method. The same as above.
/factoryreset: set settings back to default.
/gaiaserver servername.com: uses a different GAIA server to connect to Google Talk. Used for debug purposes only, there are no other known GAIA servers.
/mailto email@host.com: send an email with Gmail
/diag: start Google Talk in diagnostic mode
/log: probably has something to do with the diagnostic logging
/unregister: ?
/embedding: ?
To add these, open up your GT shortcut, and where it says "Target:" add one or more of these inside the quotations, but after the .exe part.

Emotions :

All these emotions appears in color in a conversation : (but having them in an image would be better, like iChat or MSN)

:-|
:-O
:-x
:-P
:-D
;-)
:-(
:-)
B-)
:'(
:|
:O
:x
:P
:D
:)
:(
:)

Tips and Tricks about Google

I have been quite fascinated by Google ever since they started out as a search engine. Over the time Google has become an indispensable tool for any serious geek. I will describe a few techniques that I use. They have been collected from various help pages, chat rooms and books. If you find any error please point it out.

1.) Common queries:
So how will you effectively use Google. Take this example. A friend of mine was asking me over yahoo messenger what is a blog. I told him to search on Google and find out. He tried this.
blog
Alas the answer was there but lost in thousands of links. He alerted me that he can't find an answer. I told " Ok , Let me see What is wrong? ". I tried this

What is blog
The answer was right there.

While formulating search queries you must be specific as far as possible. A query like Linux vpn howto can yield a different result than vpn howto. Google seems to be intelligent enough to understand some human thought chain. So while making queries be a bit descriptive and formulate a properly worded query. Google ignores some of the common words such as 'the' 'and' 'a' etc while performing a search.

2.) How to formulate a good search string :
Google uses AND logic for the queries by default. If you search linux vpn howto google searches for pages containing linux AND vpn AND howto. You can put OR logic in the search box like this

linux OR vpn OR howto

You can exclude some terms using "-"

linux vpn -installation

Try ( linux vpn - installation ) and see how it is different from the above query. You can group a set of terms by enclosing them in brackets. It is also possible to combine AND and OR operators.

It may be noted that Google is not case sensitive regarding search strings. Linux, LINUX and linux produces the same results. However the operators such as AND and OR are case sensitive.

3.) Searching for a file :

Suppose you want to search for pdf documents only. You can do this by attaching a filetype modifier to your query. A typical query can look like this
vpn filetype:pdf
Google can recognize most common filetyes.

4.) Searching only at a site :

This is similar to file type modifier. Try this VPN site:ibm.com
Then try vpn site:edu filetype:pdf You can get some interesting result by trying this query
linux site:microsoft.com
5.) Searching in URLs and Page titles :
You can search in urls using inurl modifier. Try inurl:smb.conf

Similarly you can use intitle modifier.

6.) Searching in Google cache :
If you are looking for some old web site you can directly search in Google cache.
Try cache:slashdot.org and see the cached pages.

7.) Looking for definitions :
This can be handy if your school going kid pesters you for his home work.
Try

define:watt
It bring out definitions from around the web.

8.) Looking at Google advanced search :
I think it is one link that most people try to ignore. It is designed for commoners. Almost all the options I mentioned above are available through advanced search.

The preferences page is also worth a visit. It uses cookies to set preferences such as number of search results to be displayed, language preferences etc.

9.) Finding the price of some geek gadget :
Google has a companion site called froogle.com. Its interface is exactly similar. But it returns prices across various stores in the US. This feature can be very useful if you plan to buy some strange hardware. Let us hope that froogle.co.in will appear soon.
10.) Google Calculator :
In the google search :
Type in an equation: (100+4567*10-200=)
What is the square root of 267? (sqrt(267))

11.) Google labs :
Google is very innovative and always tries to introduce new features. You can see some of the upcoming features at labs.google.com . Also Google conducts a puzzle champion ships annually.

12.) Google Sets :
Google sets is an interesting feature currently available at http://labs.google.com/sets.

Have a look at the page. You will see a number of text boxes. Enter some words which belongs to a set of items and search. Google will complete the set.

I typed in the names of following linux distributions " Mandrake" "Suse" " "debian" and hit larger set button. Google provided a huge list of linux distributions.

Basic of C#

C# is a language with the features of C++, programming style like Java and rapid application model of BASIC. If you already know the C++ language, it will take you less than an hour to quickly go through the syntax of C#. Familiarity with Java will be a plus, as Java program structure, the concept of packages and garbage collection will definitely help you learn C# more quickly. So while discussing C# language constructs, I will assume, you know C++.

This article discusses the C# language constructs and features using code examples, in a brief and comprehensive way, so that you just by having a glance at the code, can understand the concepts.

Note: This article is not for C# gurus. There must be some other beginner's articles on C#, but this is yet another one.

Following topics of C# language are discussed:

* Program structure
* Namespaces
* Data types
* Variables
* Operators and expressions
* Enumerations
* Statements
* Classes and structs
* Modifiers
* Properties
* Interfaces
* Function parameters
* Arrays
* Indexers
* Boxing and unboxing
* Delegates
* Inheritance and polymorphism

Following are not discussed:

* Things which are common in C++ and C#.
* Concepts like garbage collection, threading, file processing etc.
* Data type conversions
* Exception handling
* .NET library

Program structure

Like C++, C# is case-sensitive. Semi colon (;) is the statement separator. Unlike C++, there are no separate declaration (header) and implementation (CPP) files in C#. All code (class declaration and implementation) is placed in one file with extension cs.

Have a look at this Hello world program in C#.

using System;

namespace MyNameSpace

{
class HelloWorld

{
static void Main(string[] args)
{
Console.WriteLine ("Hello World");
}
}
}

Everything in C# is packed into a class and classes in C# are packed into namespaces (just like files in a folder). Like C++, a main method is the entry point of your program. C++'s main function is called main whereas C#'s main function starts with capital M and is named as Main.

No need to put a semi colon after a class block or struct definition. It was in C++, C# doesn't require that.
Namespace

Every class is packaged into a namespace. Namespaces are exactly the same concept as in C++, but in C# we use namespaces more frequently than in C++. You can access a class in a namespace using dot (.) qualifier. MyNameSpace is the namespace in hello world program above.

Now consider you want to access the HelloWorld class from some other class in some other namespace.

using System;
namespace AnotherNameSpace
{
class AnotherClass
{
public void Func()
{
Console.WriteLine ("Hello World");
}
}
}

Now from your HelloWorld class you can access it as:

using System;
using AnotherNameSpace; // you will add this using statement

namespace MyNameSpace
{
class HelloWorld
{
static void Main(string[] args)
{
AnotherClass obj = new AnotherClass();
obj.Func();
}
}
}

In .NET library, System is the top level namespace in which other namespaces exist. By default there exists a global namespace, so a class defined outside a namespace goes directly into this global namespace and hence you can access this class without any qualifier.

You can also define nested namespaces.
Using

The #include directive is replaced with using keyword, which is followed by a namespace name. Just as using System as above. System is the base level namespace in which all other namespaces and classes are packed. The base class for all objects is Object in the System namespace.
Variables

Variables in C# are almost the same as in C++ except for these differences:

1. Variables in C# (unlike C++), always need to be initialized before you access them, otherwise you will get compile time error. Hence, it's impossible to access an un-initialized variable.
2. You can't access a dangling pointer in C#.
3. An expression that indexes an array beyond its bounds is also not accessible.
4. There are no global variables or functions in C# and the behavior of globals is achieved through static functions and static variables.

Data types

All types of C# are derived from a base class object. There are two types of data types:

1. Basic/ built-in types
2. User-defined types

Following is a table which lists built-in C# types:

Type Bytes Description
byte 1 unsigned byte
sbyte 1 signed byte
short 2 signed short
ushort 2 unsigned short
int 4 signed integer
uint 4 unsigned integer
long 8 signed long
ulong 8 unsigned long
float 4 floating point number
double 8 double precision number
decimal 8 fixed precision number
string Unicode string
char Unicode char
bool true, false boolean

Note: Type range in C# and C++ are different, example, long in C++ is 4 bytes, and in C# it is 8 bytes. Also the bool and string types are different than those in C++. bool accepts only true and false and not any integer.

User defined types includes:

1. Classes
2. Structs
3. Interfaces

Memory allocation of the data types divides them into two types:

1. Value types
2. Reference types

Value types

Values types are those data types which are allocated in stack. They include:

* All basic or built-in types except strings
* Structs
* Enum types

Reference types

Reference types are allocated on heap and are garbage collected when they are no longer being used. They are created using new operator, and there is no delete operator for these types unlike C++ where user has to explicitly delete the types created using delete operator. In C#, they are automatically collected by garbage collector.

Reference types include:

* Classes
* Interfaces
* Collection types like Arrays
* String

Enumeration

Enumerations in C# are exactly like C++. Defined through a keyword enum.

Example:

enum Weekdays
{
Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday
}

Classes and structs

Classes and structs are same as in C++, except the difference of their memory allocation. Objects of classes are allocated in heap, and are created using new, where as structs are allocated in stack. Structs in C# are very light and fast data types. For heavy data types, you should create classes.

Examples:

struct Date
{
int day;
int month;
int year;
}

class Date
{
int day;
int month;
int year;
string weekday;
string monthName;
public int GetDay()
{
return day;
}
public int GetMonth()
{
return month;
}
public int GetYear()
{
return year;
}
public void SetDay(int Day)
{
day = Day ;
}
public void SetMonth(int Month)
{
month = Month;
}
public void SetYear(int Year)
{
year = Year;
}
public bool IsLeapYear()
{
return (year/4 == 0);
}
public void SetDate (int day, int month, int year)
{
}
...
}

Properties

If you are familiar with the object oriented way of C++, you must have an idea of properties. Properties in above example of Date class are day, month and year for which in C++, you write Get and Set methods. C# provides a more convenient, simple and straight forward way of accessing properties.

So above class can be written as:

using System;
class Date
{
public int Day{
get {
return day;
}
set {
day = value;
}
}
int day;

public int Month{
get {
return month;
}
set {
month = value;
}
}
int month;

public int Year{
get {
return year;
}
set {
year = value;
}
}
int year;

public bool IsLeapYear(int year)
{
return year%4== 0 ? true: false;
}
public void SetDate (int day, int month, int year)
{
this.day = day;
this.month = month;
this.year = year;
}
}

Here is the way you will get and set these properties:
class User
{
public static void Main()
{
Date date = new Date();
date.Day = 27;
date.Month = 6;
date.Year = 2003;
Console.WriteLine
("Date: {0}/{1}/{2}", date.Day, date.Month, date.Year);
}
}

Modifiers

You must be aware of public, private and protected modifiers that are commonly used in C++. I will here discuss some new modifiers introduced by C#.
readonly

readonly modifier is used only for the class data members. As the name indicates, the readonly data members can only be read, once they are written either by directly initializing them or assigning values to them in constructor. The difference between the readonly and const data members is that const requires you to initialize with the declaration, that is directly. See example code:

class MyClass
{
const int constInt = 100; //directly

readonly int myInt = 5; //directly

readonly int myInt2;

public MyClass()
{
myInt2 = 8; //Indirectly

}
public Func()
{
myInt = 7; //Illegal

Console.WriteLine(myInt2.ToString());
}

}

sealed

sealed modifier with a class don't let you derive any class from it. So you use this sealed keyword for the classes which you don't want to be inherited from.

sealed class CanNotbeTheParent
{
int a = 5;
}

unsafe

You can define an unsafe context in C# using unsafe modifier. In unsafe context, you can write an unsafe code, example: C++ pointers etc. See the following code:

public unsafe MyFunction( int * pInt, double* pDouble)
{
int* pAnotherInt = new int;
*pAnotherInt = 10;
pInt = pAnotherInt;
...
*pDouble = 8.9;
}

Interfaces

If you have an idea of COM, you will immediately know what I am talking about. An interface is the abstract base class containing only the function signatures whose implementation is provided by the child class. In C#, you define such classes as interfaces using the interface keyword. .NET is based on such interfaces. In C#, where you can't use multiple class inheritance, which was previously allowed in C++, the essence of multiple inheritance is achieved through interfaces. That's your child class may implement multiple interfaces.

using System;
interface myDrawing
{
int originx
{
get;
set;
}
int originy
{
get;
set;
}
void Draw(object shape);
}

class Shape: myDrawing
{
int OriX;
int OriY;

public int originx
{
get{
return OriX;
}
set{
OriX = value;
}
}
public int originy
{
get{
return OriY;
}
set{
OriY = value;
}
}
public void Draw(object shape)
{
... // do something

}

// class's own method

public void MoveShape(int newX, int newY)
{
.....
}

}

Arrays

Arrays in C# are much better than C++. Arrays are allocated in heap and thus are reference types. You can't access an out of bound element in an array. So C# prevents you from that type of bugs. Also some helper functions to iterate array elements are provided. foreach is the statement for such iteration. The difference between the syntax of C++ and C# array is:

* The square brackets are placed after the type and not after the variable name
* You create element locations using new operator.

C# supports single dimensional, multi dimensional, and jagged arrays (array of array).

Examples:

int[] array = new int[10]; // single-dimensional array of int

for (int i = 0; i < array.Length; i++)
array[i] = i;

int[,] array2 = new int[5,10]; // 2-dimensional array of int

array2[1,2] = 5;

int[,,] array3 = new int[5,10,5]; // 3-dimensional array of int

array3[0,2,4] = 9;

int[][] arrayOfarray = new int[2]; // Jagged array - array of array of int

arrayOfarray[0] = new int[4];
arrayOfarray[0] = new int[] {1,2,15};

Indexers

Indexer is used to write a method to access an element from a collection, by straight way of using [], like an array. All you need is to specify the index to access an instance or element. Syntax of Indexer is same as that of class properties, except they take the input parameter, that is the index of the element.

Example:

Note: CollectionBase is the library class used for making collections. List is the protected member of CollectionBase which stores the collection list.

class Shapes: CollectionBase
{
public void add(Shape shp)
{
List.Add(shp);
}

//indexer

public Shape this[int index]
{
get {
return (Shape) List[index];
}
set {
List[index] = value ;
}
}
}

Boxing/Unboxing

The idea of boxing is new in C#. As mentioned above, all data types, built-in or user defined, are derived from a base class object in the System namespace. So the packing of basic or primitive type into an object is called boxing, whereas the reverse of this known as unboxing.

Example:

class Test
{
static void Main()
{
int myInt = 12;
object obj = myInt ; // boxing

int myInt2 = (int) obj; // unboxing

}
}

Example shows both boxing and unboxing. An int value can be converted to object and back again to int. When a variable of a value type needs to be converted to a reference type, an object box is allocated to hold the value, and the value is copied into the box. Unboxing is just the opposite. When an object box is cast back to its original value type, the value is copied out of the box and into the appropriate storage location.
Function parameters

Parameters in C# are of three types:

1. By-Value/In parameters
2. By-Reference/In-Out parameters
3. Out parameters

If you have an idea of COM interface and it's parameters types, you will easily understand the C# parameter types.
By-Value/In parameters

The concept of value parameters is same as in C++. The value of the passed value is copied into a location and is passed to the function.

Example:

SetDay(5);
...
void SetDay(int day)
{
....
}

By-Reference/In-Out parameters

The reference parameters in C++ are passed either through pointers or reference operator &. In C# reference parameters are less error prone. Reference parameters are also called In-Out parameters because you pass a reference address of the location, so you pass an input value and get an output value from that function.

You can not pass an un-initialized reference parameter into a function. C# uses a keyword ref for the reference parameters. You also have to use keyword ref with an argument while passing it to a function demanding reference parameter.

Example:

int a= 5;
FunctionA(ref a); // use ref with argument or you will get compiler error

Console.WriteLine(a); // prints 20


void FunctionA(ref int Val)
{
int x= Val;
Val = x* 4;
}

Out parameter

Out parameter is the parameter which only returns value from the function. The input value is not required. C# uses a keyword out for the out parameters

Example:

int Val;
GetNodeValue(Val);

bool GetNodeValue(out int Val)
{
Val = value;
return true;
}

Variable number of parameters and arrays

Arrays in C# are passed through a keyword params. An array type parameter should always be the right most argument of the function. Only one parameter can be of array type. You can pass any number of elements as an argument of type of that array. You can better understand it from example below:

Note: This is the only way C# provides for optional or variable number of parameters, that is using array.

Example:

void Func(params int[] array)
{
Console.WriteLine("number of elements {0}", array.Length);
}


Func(); // prints 0

Func(5); // prints 1

Func(7,9); // prints 2

Func(new int[] {3,8,10}); // prints 3

int[] array = new int[8] {1,3,4,5,5,6,7,5};
Func(array); // prints 8

Operators and expressions

Operators are exactly the same as of C++ and thus the expression also. However some new and useful operators are also added. Some of them are discussed here.
is operator

is operator is used to check whether the operand types are equal or convert-able. The is operator is particularly useful in the polymorphism scenarios. is operator takes two operands and the result is a boolean. See the example:

void function(object param)
{
if(param is ClassA)
//do something

else if(param is MyStruct)
//do something

}
}

as operator

as operator checks if the type of the operands are convert-able or equal (as is done by is operator) and if it is, the result is a converted or boxed object (if the operand can be boxed into the target type, see boxing/unboxing). If the objects are not convert-able or box-able, the return is a null. Have a look at the example below to better understand the concept.

Shape shp = new Shape();
Vehicle veh = shp as Vehicle; // result is null, types are not convertable


Circle cir = new Circle();
Shape shp = cir;
Circle cir2 = shp as Circle; //will be converted


object[] objects = new object[2];
objects[0] = "Aisha";
object[1] = new Shape();

string str;
for(int i=0; i&< objects.Length; i++)
{
str = objects[i] as string;
if(str == null)
Console.WriteLine("can not be converted");
else
Console.WriteLine("{0}",str);
}

Output:
Aisha
can not be converted

Statements

Statements in C# are just like in C++ except some additions of new statements and modifications in some statements.

Followings are new statements:
foreach

For iteration of collections like arrays etc.

Example:

foreach (string s in array)
Console.WriteLine(s);

lock

Used in threads for locking a block of code making it a critical section.
checked/unchecked

The statements are for overflow checking in numeric operations.

Example:

int x = Int32.MaxValue; x++; // Overflow checked

{
x++; // Exception

}
unchecked
{
x++; // Overflow}

}
Following statements are modified:

Switch

Switch statement is modified in C#.

1. Now after executing a case statement, program flow can not jump to next case which was previously allowed in C++.

Example:
int var = 100;
switch (var)
{
case 100: Console.WriteLine(""); // No break here

case 200: Console.WriteLine(""); break;
}

Output in C++:



In C# you get compile time error:

error CS0163: Control cannot fall through
from one case label ('case 100:') to another

2. However you can do this similar to how you do it in C++:

switch (var)
{
case 100:
case 200: Console.WriteLine("100 or 200"); break;
}

3. You can also use constant variables for case values:

Example:

const string WeekEnd = "Sunday";
const string WeekDay1 = "Monday";

....

string WeekDay = Console.ReadLine();
switch (WeekDay )
{
case WeekEnd: Console.WriteLine("It's weekend!!"); break;
case WeekDay1: Console.WriteLine("It's Monday"); break;

}

Delegates

Delegates let us store function references into a variable. In C++, this is like using and storing function pointer for which we usually use typedef.

Delegates are declared using a keyword delegate. Have a look at this example, and you will understand what delegates are:

Example:

delegate int Operation(int val1, int val2);
public int Add(int val1, int val2)
{
return val1 + val2;
}
public int Subtract (int val1, int val2)
{
return val1- val2;
}

public void Perform()
{
Operation Oper;
Console.WriteLine("Enter + or - ");
string optor = Console.ReadLine();
Console.WriteLine("Enter 2 operands");

string opnd1 = Console.ReadLine();
string opnd2 = Console.ReadLine();

int val1 = Convert.ToInt32 (opnd1);
int val2 = Convert.ToInt32 (opnd2);

if (optor == "+")
Oper = new Operation(Add);
else
Oper = new Operation(Subtract);

Console.WriteLine(" Result = {0}", Oper(val1, val2));
}

Inheritance and polymorphism

Only single inheritance is allowed in C#. Multiple inheritance can be achieved using interfaces.

Example:

class Parent{
}

class Child : Parent

Virtual functions

Virtual functions to implement the concept of polymorphism are same in C#, except you use the override keyword with the virtual function implementation in the child class. The parent class uses the same virtual keyword. Every class which overrides the virtual method will use override keyword.

class Shape
{
public virtual void Draw()
{
Console.WriteLine("Shape.Draw") ;
}
}

class Rectangle : Shape

{
public override void Draw()
{
Console.WriteLine("Rectangle.Draw");
}
}

class Square : Rectangle
{
public override void Draw()
{
Console.WriteLine("Square.Draw");
}
}
class MainClass
{
static void Main(string[] args)
{
Shape[] shp = new Shape[3];
Rectangle rect = new Rectangle();

shp[0] = new Shape();
shp[1] = rect;
shp[2] = new Square();

shp[0].Draw();
shp[1].Draw();
shp[2].Draw();
}
}
Output:
Shape.Draw
Rectangle.Draw
Square.Draw

Hiding parent functions using "new"

You can define in a child class a new version of a function, hiding the one which is in base class. A keyword new is used to define a new version. Consider the example below, which is a modified version of above example and note the output this time, when I replace the keyword override with a keyword new in Rectangle class.

class Shape
{
public virtual void Draw()
{
Console.WriteLine("Shape.Draw") ;
}
}

class Rectangle : Shape
{
public new void Draw()
{
Console.WriteLine("Rectangle.Draw");
}
}
class Square : Rectangle
{
//wouldn't let u override it here

public new void Draw()
{
Console.WriteLine("Square.Draw");
}
}
class MainClass
{
static void Main(string[] args)
{
Console.WriteLine("Using Polymorphism:");
Shape[] shp = new Shape[3];
Rectangle rect = new Rectangle();

shp[0] = new Shape();
shp[1] = rect;
shp[2] = new Square();

shp[0].Draw();
shp[1].Draw();
shp[2].Draw();

Console.WriteLine("Using without Polymorphism:");
rect.Draw();
Square sqr = new Square();
sqr.Draw();
}
}

Output:
Using Polymorphism
Shape.Draw
Shape.Draw
Shape.Draw
Using without Polymorphism:
Rectangle.Draw
Square.Draw

See how the polymorphism doesn't take the Rectangle class's Draw method as a polymorphic form of the Shape's Draw method, instead it considers it a different method. So in order to avoid the naming conflict between parent and child, we have used new modifier.

Note: you can not use in the same class the two versions of a method, one with new modifier and other with override or virtual. Like in above example, I can not add another method named Draw in Rectangle class which is a virtual or override method. Also in the Square class, I can't override the virtual Draw method of Shape class.
Calling base class members

If the child class has the data members with same name as that of base class, in order to avoid naming conflicts, base class data members and functions are accessed using a keyword base. See in examples how the base class constructors are called and how the data members are used.

public Child(int val) :base(val)
{
myVar = 5;
base.myVar;
}

OR

public Child(int val)
{
base(val);
myVar = 5 ;
base.myVar;
}

Future additions

This article is just a quick overview of the C# language so that you can just become familiar with the language features. Although I have tried to discuss almost all the major concepts in C# in a brief and comprehensive way with code examples, yet I think there is lot much to be added and discussed.

In future, I would like to add more commands and concepts not yet discussed, including events etc. I would also like to write for beginners, about Windows programming using C#.
References:

* Our most commonly known MSDN
* Inside C# by Tom Archer
* A Programmer's Introduction to C# by Eric Gunnerson
* Beginning C# by Karli Watson
* Programming C# (O'Reilly)

Thursday, May 19, 2011

Enable Windows 7's all Hidden functions in one folder!!!

There is a simple way to access a hidden "God Mode" in Windows 7 and Vista. With a name like that, your expectations might be a little high -- and no, Windows is not secretly invincible -- but the trick is awesome nevertheless
"God Mode" simply provides users with a centralized Control Panel for all of Windows' settings, from changing your desktop background to setting up a VPN or partitioning your hard drive. In all, there are nearly 50 categories and 279 items and most have several entries
It's almost comical how simple it is to access it


1. Create a new folder. Anywhere is fine, I created one on my Desktop.

2. Rename the folder to: God Mode.{ED7BA470-8E54-465E-825C-99712043E01C} *Note: The "God Mode" chunk can be called anything you want.

3. The default folder icon will change to a Control Panel icon, and you can open it to view all of the settings.

User reports suggest that it may crash Windows Vista 64-bit, so proceed with caution. For what it's worth, I've successfully used the "feature" on Windows 7 Home Premium and Ultimate 64-bit.

Email Validation in c# Application

private void TextBox1_Validating(object sender, CancelEventArgs e)
{
System.Text.RegularExpressions.Regex rEmail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
if (txtEmail.Text.Length > 0)
{
if(!rEmail.IsMatch(txtEmail.Text))
{
MessageBox.Show("Please provide valid email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtEmail.SelectAll();
}
}
}

Get IP Address in C#

Using .NET 1.1 and 2.0 to retrieve the IP address
Well, in .NET 1.1 and 2.0 there are methods in the System.Net namespace that do that. Speaking of System.Net, don't forget to include the using statement, to avoid the long lines I have below:

using System.Net;

// Get the hostname
string myHost = System.Net.Dns.GetHostName();

// Get the IP from the host name
string myIP = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();

3 ways to speed up MySQL

There are ONLY 3 ways to speed up MySQL, and everything else is simply a finer point of one of these 3 ways. Here they are, in order of importance:

1. Optimize queries
2. Tune the MySQL configuration
3. Add more hardware


============================================================
#1. Query Optimization
============================================================
The most common problem with MySQL performance is unoptimized queries. Here are some examples of unoptimized queries:

- Queries that don't use indexes.
- Queries that use SELECT *.
- Queries that search full-text fields.
- Queries that are not properly limited.
- Queries that use ORDER BY unnecessarily.



Indexes
By far, the biggest problem queries are ones that don't use indexes or don't use the BEST indexes. Indexes are the key to getting the best performance out of your queries. Indexes are basically shortcuts for MySQL - they work the same way as an index in a classroom textbook. Let's say you wanted to look up all pages containing "gr8gonzo." You COULD go through every word in the book and find all the pages, but it's far faster to just flip to the index and see that "gr8gonzo" happens to be on pages 2, 6, and 32.

Most people know how to use basic indexes, but most people don't know how to use the BEST indexes. A lot of queries have more than one thing in the WHERE clause, like this:


SELECT fields FROM mytable
WHERE field1 > 123 AND field2 = 'gr8gonzo';


Most people will have an index for field1 and an index for field2. This is good, and the query will try to make use of one of those indexes (and will be faster). But if this is a query that is run frequently, it would be even better to have ANOTHER index that has BOTH field1 and field2 in it. That (usually) gives you the best query performance.

That said, you don't want to just create tons of these indexes, since each index does take a little bit of extra work for MySQL to update whenever the table changes, and those little bits can add up over time. You should really only create these multi-field indexes when there are frequent, slow queries that COULD take advantage of them. In section 2 of this article, we'll cover some ways of having MySQL tell you what queries need a tune-up, but there is one way to tell immediately if your query isn't using indexes...



EXPLAIN-ing Queries
If I wanted to see if the above query was working well, I could use EXPLAIN to do it. When you EXPLAIN a query, you're simply asking MySQL to tell you what it WOULD do if it ran the query for you. It responds with a computerish version of "Well, in order to run your query, I would use this index. That would leave me with X rows, which I would then look at in order to figure out which ones you wanted."

To EXPLAIN a query, all you have to do is run the same query but put "EXPLAIN" in front of it:

EXPLAIN SELECT fields FROM mytable
WHERE field1 > 123 AND field2 = 'gr8gonzo';

The result looks something like this:

+----+-------------+---------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | mytable | ALL | PRIMARY | NULL | NULL | NULL | 898256 | Using where |
+----+-------------+---------+------+---------------+------+---------+------+--------+-------------+
.


WHOA! At first glance, this is probably really confusing, but you can often just ignore a lot of the information, like id, select_type, table, type, and ref. And MySQL sometimes calls indexes "keys", so now let's look at the same result but without the extra columns:


+---------------+------+---------+------+--------+-------------+
| possible_keys | key | key_len | ref | rows | Extra |
+---------------+------+---------+------+--------+-------------+
| PRIMARY | NULL | NULL | NULL | 898256 | Using where |
+---------------+------+---------+------+--------+-------------+


Basically what this says is that MySQL had to go one-by-one through 898,256 rows and check each one to see if field1 > 123 and if field2 = 'gr8gonzo'. That's a lot of processing to do, especially if the final result is just a couple of rows (meaning that there are nearly 900,000 rows that get searched uselessly). Let's try adding in an index for one of those fields:


ALTER TABLE `mytable`
ADD INDEX `IDX_FIELD1` (`field1`) ;


If we re-run the EXPLAIN, we'll see:


+---------------+---------------+---------+-------+------+-------------+
| possible_keys | key | key_len | ref | rows | Extra |
+---------------+---------------+---------+-------+------+-------------+
| IDX_FIELD1 | IDX_FIELD1 | 5 | const | 1246 | Using where |
+---------------+---------------+---------+-------+------+-------------+


Well, now we're down to only looking at 1,246 rows. That's much better than 898 thousand, but we can do even better. Our query uses two fields in the WHERE clause, so we can probably gain better performance by adding in an index containing BOTH those fields:

ALTER TABLE `mytable`
ADD INDEX `IDX_FIELDS1_2` (`field1`, `field2`) ;


...and now re-run the EXPLAIN and we get.


+---------------+---------------+---------+-------------+------+-------------+
| possible_keys | key | key_len | ref | rows | Extra |
+---------------+---------------+---------+-------------+------+-------------+
| IDX_FIELDS1_2 | IDX_FIELDS1_2 | 5 | const,const | 16 | Using where |
+---------------+---------------+---------+-------------+------+-------------+


Voila! Now when we run the exact same query for real, we know that MySQL only has to search through 16 rows instead of nearly 1 million. A guaranteed speed increase, and it was free!

NOTE: In the above output, "possible_keys" will sometimes show more than one index, indicating that there's more than one choice that could help the query run faster. However the "chosen" index will be in the "key" field. The "ref" can give you an idea of how many fields are involved in the index. For example, if you have an index on one field, your "ref" column will probably just say "const" but if you have an index on two fields and both of those fields are in the WHERE clause, then you'll probably see "const,const" in the "ref" column.

ANOTHER NOTE: Whenever MySQL has to look at every row in the table, it's called a "table scan." Table scans are the slowest way MySQL can look for data. When you EXPLAIN a query, look at the "type" column - if it says "ALL" then MySQL is doing a table scan to find your data. If it says something else, like "range", then it is making use of an index. Occasionally, on small tables, MySQL will do a table scan even if you have an index. This is just MySQL knowing what is best in that situation, but you normally want to avoid these. Here's a link to the MySQL documentation on avoiding table scans:
http://dev.mysql.com/doc/refman/5.0/en/how-to-avoid-table-scan.html

There's a lot of in-depth optimization stuff on how to use EXPLAIN. If you feel like reading documentation, check here:
http://dev.mysql.com/doc/refman/5.0/en/using-explain.html

I also recently found and highly recommend a free MySQL manager application called HeidiSQL that (among other things) makes it easy to create and update your indexes. Plus, when you add indexes, it will show you the SQL code it ran to create those indexes, making it a useful learning tool.
http://www.heidisql.com/

There's also phpMyAdmin, which is installed on a lot of web hosts:
http://www.phpmyadmin.net


Using SELECT *
I'm guilty of it. It's far easier to write queries that use SELECT * and not have to worry about typing out 10 fields names, but it COULD be the culprit that slows down your web application. Here's a common mistake:

Let's say you run a web site that collects stories written by your members. All the stories are put into one big table called stories. So far so good. But now let's say you have a query out there that is used to create a menu to link to all the stories:



SELECT * FROM stories;


Well, if the CONTENTS of each story is in the stories table, then whenever you run the above query, MySQL is also sending every letter of every story in the system back to your script. If you have 1,000 stories that are about 10k each, then everytime someone views the menu, your script is downloading 10 megabytes of extra data that it just throws away without using. What a waste!

Instead, try changing your query to something like:

SELECT storyID,storyName,storyDate FROM stories;


Now we're only selecting a few fields that we need for the menu. Get into the habit of specifying ONLY the fields your script needs, and you'll find that it's easier than you think, and your scripts WILL run faster.

TIP: There's a quick way to see a summary of all the fields in the table and what type of field they are:


DESCRIBE mytable;


The Full Text
Let's stick with the "stories" example above. People will probably want to search through stories for specific words. If your story content is in a full-text field (e.g. TEXT datatype), then chances are that you're searching like this:


SELECT storyID FROM stories
WHERE storyContent LIKE '%fondled the hubcaps%';

This probably runs quickly when you don't have many stories, but it'll get slower and slower and slower over time. In this case, consider an open-source product called Sphinx Search:
http://sphinxsearch.com/

It specializes in taking your full-text content and making it searchable. A query that takes 10 seconds to run in MySQL could take 0.1 seconds in Sphinx, and that is not an exaggeration. The downside is that it is a separate program / daemon, and requires a bit of know-how to set up and get running, but it's worth the time. They have community forums to help, and some people here at Experts Exchange (like me), can also help.



Add LIMITs
This one's simple - if you only need a couple of rows out of thousands that are being returned (e.g. getting the top 10 of something), then add a LIMIT clause to the end of your query:


SELECT storyID FROM stories
ORDER BY storyRating DESC
LIMIT 10;


It can sometimes be useful to run a query that counts the number of rows in your result before pulling all of them. This can give you an idea of how to limit your rows or how to run your next query (although this largely depends on your particular situation). Here's a way to quickly get the number of stories from our example:



SELECT COUNT(storyID) AS storyCount FROM stories;


The results will be a row containing a field called "storyCount." This type of technique becomes more useful as your database grows larger and larger.



The ORDER BY Dilemma
Using ORDER BY is great for sorting, but sometimes it can create real slowdowns on MySQL. When you ORDER BY a field, MySQL first finds all the rows that will be in your results, and THEN goes back and re-orders them according to that ORDER BY field. If you have a lot of rows, then MySQL has to do a lot of re-ordering, which can be very slow.

In the above example on LIMITs, the query would have to sort every single story by its rating before returning the top 10. However, if I know that all of the top 10 stories have a rating of 4 or higher, then I could reduce the number of stories to be sorted like this:


SELECT storyID FROM stories
WHERE storyRating >= 4
ORDER BY storyRating DESC
LIMIT 10;


Now MySQL may only have to sort through 100 stories instead of 10,000.

Sometimes it's worth asking yourself whether you REALLY need to use ORDER BY at all. Sometimes it's faster to skip the ordering info altogether on the database and use PHP or something else to handle the sorting (although MySQL is usually faster at it).

One other trick is to create an index of the fields you're SELECTing and ORDERing BY. So if you had a query:


SELECT storyID,storyRating FROM stories
ORDER BY storyRating DESC;


Then, the query could benefit a lot from an multi-field index of storyID and storyRating.



============================================================
#2. The MySQL Configuration
============================================================
There are a LOT of ways to configure MySQL, but all of them start with the my.cnf configuration file (that's usually what it's called). Generally speaking, you can tune up MySQL by telling it to cache things in memory. When it stores any data in memory, MySQL can access it almost instantly instead of having to go back to the full database on the hard drive and look up the requested data (which is slow).

Here's an example section of a my.cnf file (I've taken out some extra parameters that weren't performance-related and some others that I won't discuss in this article):


[mysqld]
skip-name-resolve

query_cache_size = 16M

log-slow-queries=/var/log/slowqueries.log
long_query_time = 4
log-queries-not-using-indexes

table_cache = 512
tmp_table_size = 128M
max_heap_table_size = 128M
myisam_sort_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 256K
key_buffer = 128M
Toggle HighlightingOpen in New WindowSelect All

The first thing I always do is disable name resolution (skip-name-resolve). Basically, name resolution just tries to look up a "caller ID" on whoever is connecting to the database. I still don't know why it's enabled by default. It's not only a potential security problem, but it's usually unnecessary for most web server setups (since the web server is the one that does the connecting, not the visitors), and it has the potential to crash the system (if your DNS goes down for a while and MySQL gets filled up with connections that are waiting to be "resolved").

Next, enable the query cache (query_cache_size). In the above example, I've got a 16-megabyte query cache. Basically, if I run a query that takes 5 seconds to run, and then I refresh a page or something (causing the query to run again), then the query will run instantly because MySQL will remember the results of the query from the first time. If the tables involved in the query get changed, though, then it will clear any cached results that use those tables (so you're always getting accurate data). Start with a 16-megabyte cache and work your way up as necessary (I'll explain in a bit how to tell when to increase the cache).

Third, enable the slow query log (log-slow-queries and long_query_time and log-queries-not-using-indexes). This tells MySQL to keep track of all the queries that take longer than a certain number of seconds (long_query_time) to complete. The log-queries-not-using-indexes option also includes queries that don't use indexes (simple enough). Just let the log sit for a day or two while you use your application, and then look at it to find all the queries that need to be optimized.

The last section of lines have several different purposes (caching joins, ORDER BY results, temporary tables, etc), which all affect speed, but it's difficult to know exactly what values to use sometimes. That's why I recommend using MySQLTuner:
http://wiki.mysqltuner.com/MySQLTuner

It's a Perl script that you just download and run on your database server after letting your server run for a few days (without restarting). The script will look at all the statistics that MySQL collects and will make recommendations on what to change in your my.cnf file to make things run better (like increasing query cache size or table_cache and that type of thing). It's pretty straightforward and doesn't take long to run.



============================================================
#3. Instant Speed! Just Add Hardware!
============================================================
This is usually the most obvious answer. Upgrade to a faster CPU, add more RAM, etc... and you'll run faster. This is true, but there are a few things to know first.

First, the NUMBER of hard drives is more important than the SPACE. Some people make the mistake of getting two 1-terabyte drives and just using those to run their database server. By adding multiple hard disks in a RAID array (which is what most servers use anyway), you're effectively distributing the load.

If two queries are running at the same time and you only have two hard drives, then there's a good chance that the data for both queries is located on the same hard drive. Since a hard drive can only do one thing at a time, one of the queries will have to wait a little bit longer for the other one to finish before it can run. But if you have, say, 6 hard drives or more (the more the merrier), then one query might need data from Hard Drive #2 while the second query needs data from Hard Drive #5. Both hard drives can work at the same time and send data back nearly simultaneously. At least that's the gist of it, so spend money on multiple, fast hard disks for that extra bump in speed. Hard disks are usually the biggest speed bottleneck anyway (hardware-wise).

Last point on hard disks - if someone else is setting up your server and wants to know what RAID level you want to use, try to use RAID 10 (safe + good performance). Otherwise, use RAID 1 (safe). Other RAID levels have their advantages and disadvantages, but those are my standard recommendations.

Second, there's usually not a direct correlation between RAM and speed. Just because you add more RAM doesn't mean the system automatically uses it (or uses it right). If you've got several gigabytes of RAM already, then any additional RAM should probably go towards bigger caches. There are other uses, as well (like increasing the number of maximum connections), but if you're reading this article, then you may not be at that point yet anyway.

Third, CPU is sort of like luck - it affects everything a little bit, and affects some things a LOT. If you're writing a math-hungry application, crunching statistics, etc... then investing in the latest and greatest CPU and motherboard may be a good choice. If it's just your standard web/database server, then there are probably better ways of spending your money (-cough- more hard drives -cough-).

Finally, if one server just can't handle all the traffic, consider setting up another server with dual-master replication for a quick-n-dirty way of load-balancing. (Note - replication doesn't actually do load-balancing, it just keeps two servers in complete, realtime sync so you can send 50% of visitors to one database server and the other 50% to the other server. It also makes for a handy backup / failover system.)
http://www.neocodesoftware.com/replication/


FINAL TIP: A lot of places will have a test database server and a real database server, but the test database server only has very few rows in it (just enough for a basic test). This makes it easy to misjudge your application's REAL performance. Try to make sure that your test database has similar data to your real database so you get a better picture of how your queries will perform in the real world. Many MySQL manager programs like phpMyAdmin and HeidiSQL make it easy to download all the data from your real database so you can upload it into your test database. (There's also a command line tool called mysqldump.)

That's it! If you've read this far, then you know all the major (and some minor) steps in improving your MySQL performance, and you're now further ahead than most others who are still trying to read through those boring, thick manuals. If you run into any trouble with any of the points made here, please feel free to comment and/or post questions in the Experts Exchange MySQL area.