November 2005 - Posts

Currently im working on project which requires more R & D stuff.. so guys stick to my blog to get latest. This might be pretty simple but as we all know its worth to keep them where we can access anytime. I had a requirment to convert a color image to grayscale. So today i will show you how to do that... below is the code sample. Remember to import System.Drawing.

public static Bitmap ConvertToGrayscale(string source)
        {
            Bitmap b = new Bitmap(source);
            Bitmap bm = new Bitmap(b.Width,b.Height);

            for(int y=0;y<bm.Height;y++)
            {
                for(int x=0;x<bm.Width;x++)
                {
                    Color c=b.GetPixel(x,y);
                    int luma = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);
                    bm.SetPixel(x,y,Color.FromArgb(luma,luma,luma));
                }
            }
            return bm;
        }



Its Microsoft shared source Common Language Infrastructure(aka CLI) implementation. Actually it’s a code which distribute freely to the community to find out what’s going on under the hood. Specially it comes with the C# compiler(written in C++) and Jscript complier which written purely in C#. And there are many more. You can download it from the MSDN.

 http://www.microsoft.com/downloads/details.aspx?FamilyId=3A1C93FA-7462-47D0-8E56-8DD34C6292F0&displaylang=en

Posted by Ludmal De Silva | with no comments

Recently I had a requirement to save encrypted password in the database, so I thought to share the code and the concepts.  Basically there are two ways of encryption. One-way encryption and two-way encryption. Im going to talk about one-way encryption, which uses the MD5 algorithm. Basically what im trying to do here is to connect to the database and save the username & password (encrypted) in the testuser table. You will find the below code example with comments. Database part is pretty straight forward. You have to focus on the encryption part. To encrypt the password im going to use the MD5CryptoServiceProvider class. Remember to use the System.Security.Cryptography.  When you create the password field in the databast table use the binary[16].
  
     string username= "ludmal";
    string password = "oncelifeoncechance";
    //Creating connection
    SqlConnection cnn = new SqlConnection("SERVER=LOCALHOST;UID=SA;PWD=;INITIAL CATALOG=TEST");
    SqlCommand cmd = new SqlCommand("INSERT INTO TESTUSER VALUES (@Username, @Password)", cnn);
           
    cmd.Parameters.Add("@Username", username);
       
    //Now this is the encryption part.
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    byte[] hashedBytes;
    UTF8Encoding enco = new UTF8Encoding();
    hashedBytes = md5.ComputeHash(enco.GetBytes(password));
    //encryption done
           
    //Do the database operations
    cmd.Parameters.Add("@Password", hashedBytes );
   
    cnn.Open();
    cmd.ExecuteNonQuery();
    cnn.Close();   

Currently im working on a project which we have to download the windows application from the browser and run the application. Download part is pretty simple. So to run the application(exe) which is already reside in the client system you have to use WScript.shell ActiveX object. This will provide access to the native windows shell.  Below is the code sample for you to run notepad.exe from your browser.

function Show() {
     var test = new ActiveXObject( 'WScript.Shell' )
    test.Run('%systemroot%\\system32\\notepad.exe',1,true); }

You have to set the security low in order to create the activex object. You can set the security in IE by clicking Tools > Internet Options > Security
and then click Custom Level and then Reset-To: Low.

Hi folks!! Im just planning to bring my blog to .netforum.lk.. any ideas..???

Great !!! Tks wela.. for the quick response. we do need MVPs like u..  ;)

cheers!!!