Thursday, May 14, 2015

Image Split

public static void ImageSplit(string Filname)
        {
            // string Filname = @"E:\dodge_image\2.tif";
            Image img = Image.FromFile(Filname); // a.png has 312X312 width and height
            //int widthThird = (int)((double)img.Width / 3.0 + 0.5);
            //int heightThird = (int)((double)img.Height / 3.0 + 0.5);

            int widthThird = (int)((double)img.Width / 8.0 + 3.0);
            int heightThird = (int)((double)img.Height / 8.0 + 3.0);

            int k = 0;
            Bitmap[,] bmps = new Bitmap[8, 8];
            for (int i = 0; i < 8; i++)
                for (int j = 0; j < 8; j++)
                {
                    bmps[i, j] = new Bitmap(widthThird, heightThird);
                    Graphics g = Graphics.FromImage(bmps[i, j]);
                    g.DrawImage(img, new Rectangle(0, 0, widthThird, heightThird), new Rectangle(j * widthThird, i * heightThird, widthThird, heightThird), GraphicsUnit.Pixel);
                    g.Dispose();
                    if (!Directory.Exists("E:\\output\\" + Path.GetFileNameWithoutExtension(Filname)))
                        Directory.CreateDirectory("E:\\output\\" + Path.GetFileNameWithoutExtension(Filname));
                    bmps[i, j].Save("E:\\output\\" + Path.GetFileNameWithoutExtension(Filname) + "\\" + Path.GetFileNameWithoutExtension(Filname) + "_" + k + ".bmp", ImageFormat.Bmp);
                    k++;
                }

        }

Wednesday, May 6, 2015

Read from SQL Table and Write as XML File




Read from SQL Table and Write as XML File:

 string Coon = ConfigurationManager.ConnectionStrings["ConnectionSQL"].ConnectionString;

            SqlDataAdapter DA = null;
            DataSet ds = new DataSet();
            DataTable dt = null;
            SqlConnection Con = new SqlConnection(Coon);
            string Query = "SELECT ID,NAME,SALARY,CITY FROM Customer FOR XML                    RAW('Customer'), ROOT('Customers'), ELEMENTS";

            XmlReader reader;
            XmlDocument xmlDoc;
            SqlCommand cmd = null;

            cmd = new SqlCommand(Query, Con);
            Con.Open();
            reader = cmd.ExecuteXmlReader();
            xmlDoc = new XmlDocument();
            while (reader.Read())
            {
                xmlDoc.Load(reader);
            }
            if (File.Exists(@"C:\sample.xml"))
            {
                File.Delete(@"C:\sample.xml");
                xmlDoc.Save(@"C:\sample.xml");
            }
            else
            {
                xmlDoc.Save(@"C:\sample.xml");
            }

            Con.Close();

            MessageBox.Show("Completed");


Read XML File and  Display in List

     XmlReader xReader = XmlReader.Create(@"E:\sample.xml");
            while (xReader.Read())
            {
                switch (xReader.NodeType)
                {
                    case XmlNodeType.Element:
                        listBox1.Items.Add("<" + xReader.Name + ">");
                        break;
                    case XmlNodeType.Text:
                        listBox1.Items.Add(xReader.Value);
                        break;
                    case XmlNodeType.EndElement:
                        listBox1.Items.Add("</" + xReader.Name + ">");
                        break;
                }
            }

Encrypt and Decrypt Web.config File

For Encryption:


Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            ConfigurationSection configSection =config.GetSection("connectionStrings");
            if (!configSection.SectionInformation.IsProtected)
            {
                configSection.SectionInformation.
                    ProtectSection("DataProtectionConfigurationProvider");
                config.Save();
                Response.Write("ConnectionStrings has been encryted successfully.");
            }
            else
            {
                Response.Write("ConnectionStrings has been encryted, this action has been cancled");
            }





For Decryption:

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            ConfigurationSection configSect = config.GetSection("connectionStrings");
            if (configSect.SectionInformation.IsProtected)
            {
                configSect.SectionInformation.UnprotectSection();
                config.Save();
                Response.Write("ConnectionStrings has been Decrypted successfully.");

            }