Powered By Blogger

C#SHARP ANSWER

Friday, June 24, 2011

extracting all .jpg images from a folder

trying to extract all the images from a folder then follow up the following code and extract all the .jpg images from a folder

 String[] athar = Directory.GetFiles(@"D:\\", "*.JPG");
        PictureBox[] pb = new PictureBox[300];

int j = 100, l = 0,k=0;
    for (k=0; k <300; k++)
    {
        pb[k] = new PictureBox();
        if (k % 5 == 0)
        {
            j += 200;
            l = 0;
        }
        pb[k].Location = new Point((l * 190) + 200, j);
        pb[k].Size = new Size(150, 100);
        pb[k].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
        pb[k].BackColor = Color.Black;
        pb[k].BorderStyle = BorderStyle.FixedSingle;
        pb[k].Image = Image.FromFile(athar[k]);
        pb[k].Visible = true;
        this.Controls.Add(pb[k]);
        pb[k].Cursor = System.Windows.Forms.Cursors.Hand;

        // Panel1.Controls.Add(pb[i]);

        pb[k].BringToFront();
      l++;
    }

creating thumbnail of a image

Feeling that you website is taking much time to load due to heavy size images then i have a solution to make your website load faster by just creating thumbnail of those images you want to add to your website.
C# code to create thumbnail of image is as follows

            string tostore="D:\\DSCF2088";
            int ImageHeight=200,n=0;
            foreach (string at in athar)
            {
                System.Drawing.Image i = System.Drawing.Image.FromFile(at);
                i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                System.Drawing.Image th = i.GetThumbnailImage
                                    (
                                    ImageHeight * i.Width / i.Height,
                                    ImageHeight,
                                    new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),
                                    IntPtr.Zero
                                    );
                i.Dispose();
                EncoderParameters ep = new EncoderParameters(1);
                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)80);
                ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
                th.Save(tostore+n+".jpg", ici, ep);
                th.Dispose();
                n++;
            }

Wednesday, June 15, 2011

c# code to copy the file

using System;
using System.IO;

class FileCopy
{
  public static void Main() 
  {
    string sourceFile = @"C:\SourceFile.txt";
    string destinationFile = @"C:\DestinationFile.txt";

    //Attempt file copy
    try 
    {
      //Delete destinationFile for overwrite,
      //causes exception if already exists.
      File.Delete(destinationFile);

      File.Copy(sourceFile, destinationFile);
       
      //Successful if the method gets this far without causing an exception
      Console.WriteLine("Copy completed.");
    } 

    //If the file copy fails then catch will gain control of the method
    catch (Exception exc)
    {
      Console.WriteLine(exc.Message);
    }

    Console.WriteLine("\nPress Enter to Continue:");
    Console.Read();
  }
}

Saturday, June 4, 2011

What c# Brings to the table


        given the .NET is such a radical departure from the current thoughts of the day,Microsoft has developed a new programming language(C#) specifically for this new platform. C# is programming language that looks very similar to syntax of java. for example like java a c# class definition is contained within a single-source code file (*cs) rather than c++ centric view of splitting a class definition into discrete header(*h) and implementation (*cpp) files. however ,to call c# a java rip-off is inaccurate. both c# and java are based on the syntactical construct of c++.just as java is in many way a cleaned version of java-after all they are all in the same family of languages.
 

Friday, June 3, 2011

Features of C# Language


No pointers required ! C# programs typically have no need for
direct pointer manipulation.
 Automatic memory management.
 Formal syntactic constructs for enumerated structures and class
properties.
The C++ like ability to overload operators
Full support for interface based programming techniques
Full support for aspect based programming technique via
attribute..

Tuesday, May 31, 2011

configuring the c# compiler

Transcend 4 GB Class 6 SDHC Flash Memory Card TS4GSDHC6
Before you can begin to make use of the c# command line compiler,you need to ensure that your development machine recognizes the existence of csc.exe.Ideally, csc.exe is configured correctly at the time you install .NET SDK. However ,in my experiences teaching various .NET classes,I have found out the hard way (typically during the first code demo) this is not always the case . If  csc.exe is not configured correctly ,you are forced to change to the directory containing csc.exe before you can compile your program ( which is massive pain in the neck, given that you must type in the full path name to the *.cs files to be compiled ).
     To equip development machine to compile *.cs files from any subdirectory, follow these steps
  • Right-click the My computer icon and select properties from the pop up menu.
  • select the advanced tab and click the environment variable button.
  • Double click the path variable from the system variable list box.
  • Add the following line to the end of the current value (note each value in the path variable is separated by a semicolon) :\%windir%\Microsoft.NET\Framework\v1.1.4322
ofcourse the exact path name may need to be adjusted based on your current version and location of the .NET SDK (so be sure to do sanity check). once you have
added the correct path setting, you may take a test run by closing any command windows open in the background ( to commit the settings),then open a new command window and enter csc -?
If you set things up correctly,you should see a display of each option supported by the raw c# compiler

Sunday, May 29, 2011

c# code to extract source of web page


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
        // used to build entire input
        StringBuilder sb = new StringBuilder();

        // used on each read operation
        byte[] buf = new byte[8192];

        // prepare the web page we will be asking for
        HttpWebRequest request = (HttpWebRequest)
            WebRequest.Create("http://www.google.co.in/search?hl=en&biw=772&bih=493&q=operating+system&oq=operating&aq=0&aqi=g10&aql=f&gs_sm=e&gs_upl=6031l10625l0l9l8l0l1l1l0l359l1202l2-1.3");

        // execute the request
        HttpWebResponse response = (HttpWebResponse)
            request.GetResponse();

        // we will read data via the response stream
        Stream resStream = response.GetResponseStream();

        string tempString = null;
        int count = 0;

        do
        {
            // fill the buffer with data
            count = resStream.Read(buf, 0, buf.Length);

            // make sure we read some data
            if (count != 0)
            {
                // translate from bytes to ASCII text
                tempString = Encoding.ASCII.GetString(buf, 0, count);

                // continue building the string
                sb.Append(tempString);
            }
        }
        while (count > 0); // any more data to read?

        // print out page source
        System.IO.StreamWriter file1 = new System.IO.StreamWriter("D:\\test.txt");
        file1.WriteLine(sb.ToString());
        file1.Close();