Wednesday, May 14, 2008

Internet Explorer contains Dipak Bhattarai in title

      Recently I found many pcs affected by this. You can see Dipak Bhattarai in title of every Internet explorer window. The simples solution I found to remove this is change the registry value. I am not sure that this has also added some malicious entry some where and maybe stilling or sniffing the data. However to remove name from title , Go to Start - Run - type regedit. Now go to HEY_CURRENT_USER - SOFTWARE - MICROSOFT - INTERNET EXPLORER - MAIN

       Go to Window_Title key and change the value field to "Microsoft Internet Explorer".

Happy Programming !!!

Friday, May 09, 2008

Generate Image from text using C# OR Convert Text in to Image using C#

     Today I learn a new thing, how to generate and image form given text or how to convert text in to image? Dotnet framework provides System.Drawing and System.Drawing.Graphics class which helps us to generate image from text or convert text into image. Below is the code,

   1: private Bitmap CreateBitmapImage(string sImageText)
   2: {
   3:     Bitmap objBmpImage = new Bitmap(1, 1);
   4:  
   5:     int intWidth = 0;
   6:     int intHeight = 0;
   7:  
   8:     // Create the Font object for the image text drawing.
   9:     Font objFont = new Font("Arial", 20, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
  10:  
  11:     // Create a graphics object to measure the text's width and height.
  12:     Graphics objGraphics = Graphics.FromImage(objBmpImage);
  13:     
  14:     // This is where the bitmap size is determined.
  15:     intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
  16:     intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;
  17:  
  18:     // Create the bmpImage again with the correct size for the text and font.
  19:     objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
  20:  
  21:     // Add the colors to the new bitmap.
  22:     objGraphics = Graphics.FromImage(objBmpImage);
  23:  
  24:     // Set Background color
  25:     objGraphics.Clear(Color.White);
  26:     objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
  27:     objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
  28:     objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
  29:     objGraphics.Flush();
  30:  
  31:     return (objBmpImage);
  32: }

Fig (1) - generate image from text or convert text into image 


Happy Programming!!! 

Tuesday, May 06, 2008

Missing templates in Visual Studio installed templates

     

         Today, when I start visual studio and click on Add new Item I found that WebForm option is missing from visual studio templates. Thanks to Eric Hammersley who gave the perfect solution. To add missing templates you need to run following command on visual studio command prompt.

     Close all instance of Visual Studio. Open visual studio command prompt and type,

                devenv /installvstemplates

       Press Enter. Let the process be complete and now open visual studio. You will get all missing templates under Visual Studio installed templates. 

Happy Programming !!

Monday, May 05, 2008

How to Serialize Object in Dot Net

What is Serialization?

     Serialization is the process of storing the state of an object to the stream of bytes. Using serialization one can store the object in to memory stream or file system in such a way that the original object can be retrieve from persistent medium using DeSerialization.

     One can make the class serialize by adding [Serializable] attribute at the top of class and the dot net framework automatically serialize object of that class when ever required. For example you can store only serializable data in ViewState. So if you have a class which has [Serializable] attribute, the dot net framework automatically serialize it and store it in ViewState. You  do not have to write any code for serialization.

      In this article I used a Person class (shown in Fig - (1)) which has few fields with different datatypes. We will Serialize and Deserialize the object of Person class using CustomSerialization class (shown in Fig (2)).

   1: /// <summary>
   2: /// Summary description for Person
   3: /// </summary>
   4: [Serializable]
   5: public class Person
   6: {
   7:     private string mstrName;
   8:     private string mstrCity;
   9:     [NonSerialized]
  10:     private string mstrPhone;
  11:     private int mintPinCode;
  12:     
  13:  
  14:     public int PinCode
  15:     {
  16:         get { return mintPinCode; }
  17:         set { mintPinCode = value; }
  18:     }
  19:     
  20:     public string Phone
  21:     {
  22:         get { return mstrPhone; }
  23:         set { mstrPhone = value; }
  24:     }
  25:     
  26:     public string City
  27:     {
  28:         get { return mstrCity; }
  29:         set { mstrCity = value; }
  30:     }
  31:     
  32:     public string Name
  33:     {
  34:         get { return mstrName; }
  35:         set { mstrName = value; }
  36:     }
  37:     
  38:     
  39: }

Fig - (1) Person class which we will use for serialization.


   One can add [NonSerialized] attribute for the field that one do not wants to serialize. You can see that we have used [NonSerialized] attribute to mstrPhone field.


   Below is CustomSerialization class which includes the method for Serialization and DeSerialization. We need to import System.Runtime.Serialization and System.Runtime.Serialization.Formatters.Binary namespace.



   1: using System;
   2: using System.Data;
   3: using System.Configuration;
   4: using System.Web;
   5: using System.Web.Security;
   6: using System.Web.UI;
   7: using System.Web.UI.WebControls;
   8: using System.Web.UI.WebControls.WebParts;
   9: using System.Web.UI.HtmlControls;
  10: using System.IO;
  11: using System.Runtime.Serialization;
  12: using System.Runtime.Serialization.Formatters.Binary;
  13:  
  14:  
  15: /// <summary>
  16: /// Summary description for CustomSerialization
  17: /// </summary>
  18: public class CustomSerialization
  19: {
  20:     /// <summary>
  21:     /// Serialize Data
  22:     /// </summary>
  23:     /// <param name="objData">Data to be Serialized</param>
  24:     /// <returns>MemoryStream</returns>
  25:     public static MemoryStream SerializData(Object objData)
  26:     {
  27:         MemoryStream msMemoryStream = new MemoryStream();         
  28:  
  29:         IFormatter objIFormatter = new BinaryFormatter();
  30:         objIFormatter.Serialize(msMemoryStream, objData);
  31:         msMemoryStream.Position = 0;
  32:  
  33:         return msMemoryStream;
  34:     }
  35:  
  36:     /// <summary>
  37:     /// Deserialize Data
  38:     /// </summary>
  39:     /// <param name="msData">Memory Stream containing serialized data</param>
  40:     /// <returns>Object</returns>
  41:     public static object DeSerializData(MemoryStream msData)
  42:     {
  43:         IFormatter objIFormatter = new BinaryFormatter();
  44:         return objIFormatter.Deserialize(msData);
  45:     }
  46: }

Fig - (2) Custom Serialization class to serialize and deserialize the data.


   Code shows a part of web page code behind which shows code for serialization.



   1: public Person objPerson
   2:    {
   3:        get 
   4:        {
   5:            if (ViewState["objPerson"] == null)
   6:                ViewState["objPerson"] = new Person();
   7:            return (Person)ViewState["objPerson"]; 
   8:        }
   9:        set 
  10:        { 
  11:            ViewState["objPerson"] = value; 
  12:        }
  13:    }
  14:  
  15:    public MemoryStream msStreamData
  16:    {
  17:        get
  18:        {
  19:            if (ViewState["msStreamData"] == null)
  20:                ViewState["msStreamData"] = new MemoryStream();
  21:            return (MemoryStream)ViewState["msStreamData"];
  22:        }
  23:        set
  24:        {
  25:            ViewState["msStreamData"] = value;
  26:        }
  27:    }
  28:    
  29:  
  30:    protected void Page_Load(object sender, EventArgs e)
  31:    {
  32:        if (!Page.IsPostBack)
  33:        {           
  34:            objPerson.Name = "Chirag";
  35:            objPerson.City = "Ahmedabad";
  36:            objPerson.Phone = "1234";
  37:            objPerson.PinCode = 3456;            
  38:        }
  39:    }
  40:  
  41:    protected void btnSerializeData_Click(object sender, EventArgs e)
  42:    {
  43:        msStreamData = CustomSerialization.SerializData(objPerson);
  44:    }
  45:  
  46:    protected void btnDeSerializeData_Click(object sender, EventArgs e)
  47:    {
  48:        Person objData = (Person)CustomSerialization.DeSerializData(msStreamData);
  49:    }

Fig - (3) Web Page code behind class


     One thing to note here is, the constructor of Person class will not called when the object is deserialize. 


Happy Programming !!!