Archive

Archive for February, 2012

Photo Resizer program - Csharp Tutorial

February 17th, 2012

Basic Problem - Recent advent of high megapixel has created one problem - large image sizes. Today the Camera's come at 15 Megapixels and higher. There are few issues with large size pictures

1. It takes longer to upload to facebook because of the size.
2. It is impractical to send large size image using email.
3. Viewing large size creates issues when viewing it in some photo viewer.

When you transfer the photos from your camera to your computer, it gets stored in a directory. We wish to write a simple program that will create a new directory and will put all the resized images in the new directory.

Here is goes.

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Tutorial
{
class ImageResize
{
enum Dimensions
{
Width,
Height
}

[STAThread]
static void Main(string[] args)
{
//set a working directory
string WorkingDirectory = @"C:\Users\abhinsv\Desktop\Imageresize_src";

string[] array1 = Directory.GetFiles(WorkingDirectory,"*.jpg");

Console.WriteLine("Enter the percentage you wish to compress");
int pc = Convert.ToInt32 (Console.ReadLine());

System.IO.Directory.CreateDirectory(WorkingDirectory + @"\images" + pc.ToString() + @"\");

foreach (string name in array1)

{

Image imgPhotoVert = Image.FromFile(name);
Image imgPhoto = null;

imgPhoto = ScaleByPercent(imgPhotoVert, pc);
imgPhoto.Save(WorkingDirectory + @"\images"+ pc.ToString()+ @"\" + Path.GetFileName(name), ImageFormat.Jpeg);
imgPhoto.Dispose();

}

}
static Image ScaleByPercent(Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent / 100);

int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;

int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);

grPhoto.Dispose();
return bmPhoto;
}

}

}

If you are uninitiated, you may like to take CSharp Tutorial.

Uncategorized

How to find list of files in a directory in Csharp - A tutorial

February 17th, 2012

In many cases you need to read the file names in a directory to be able to process or take some action to process the files. Here is a simple program that will list out the files in a directory.

using System;
using System.IO;

class Program
{
static void Main()
{
// Put all file names in root directory into array.
string WorkingDirectory = @"C:\Users\abhinsv\Desktop\Imageresize_src\";
string[] array1 = Directory.GetFiles(WorkingDirectory,"*.jpg");

Console.WriteLine("--- List of the Files: ---");
foreach (string name in array1)
{
Console.WriteLine(name);

}

Console.ReadLine();

}
}

Pay special attention to

Directory.GetFiles(WorkingDirectory,"*.jpg");

It will list out all the files in the directory with the extension jpg. If you wish to list all files, irrespective of the extension, you may use

Directory.GetFiles(WorkingDirectory);

It will list all files with in the directory WITH path. Here is a typical output

--- List of the Files: ---
C:\Users\abhinsv\Desktop\Imageresize_src\imageresize_horiz.jpg
C:\Users\abhinsv\Desktop\Imageresize_src\imageresize_vert.jpg


If you want to list only the names of the file, you can use

Console.WriteLine(Path.GetFileName(name));

in place of

Console.WriteLine(name);

This will, for example give the following output

--- List of the Files: ---
imageresize_horiz.jpg
imageresize_vert.jpg

If you are uninitiated, you may like to take CSharp Tutorial.

Uncategorized

Create a new directory - Csharp Tutorial

February 17th, 2012

Creating a new directory is very simple using Csharp.  The following statement will create a new directory placed in the path C:\directory\subdirectory\. If the directory already exists with files in it, it will not be deleted and the files will not be removed.

System.IO.Directory.CreateDirectory("C:\directory\subdirectory\newdirectory);

using System;

namespace Tutorial
{
class CreateDirectory
{
static void Main(string[] args)
{
//set a working directory
string WorkingDirectory = @"C:\Users\abhinsv\Desktop\Imageresize_src";
System.IO.Directory.CreateDirectory(WorkingDirectory + @"\images");
}

}
}

If you are uninitiated in C Sharp, you may like to check CSharp Tutorial for beginners.

Here is the complete code to do this.

Uncategorized

PHP Tutorial - checkbox form

February 5th, 2012

Let us assume that you want to present a user with two or more than 2 choices and want him to take action based upon one or more selections he makes in the checkboxes.

To give you more exact example look at the following picture, where you are presented to choose one or more programming languages that you like.

Let us say you choose PHP and Javascript

If you hit the submit button the program should give the output something similar to

Here is the example code to do it.
<code>
<form action="check_programs.php" method="post">
Which Programmming Languages do you like ? <br />
<input type="checkbox" name="chsarp" value="No" /> Csharp <br />
<input type="checkbox" name="php" value="Yes" /> PHP <br />
<input type="checkbox" name="javascript" value="Yes" /> Javascript<br />
<input type="submit" name="checkform" value="Submit" />
</form>

<?
if (isset($_POST['checkform']))

{
if  ($_POST['csharp'] == 'Yes')
{
echo "You like csharp"; echo "<br />";
}

if  ($_POST['php'] == 'Yes')
{
echo "You like PHP";echo "<br />";
}

if  ($_POST['javascript'] == 'Yes')
{
echo "You like javascript"; echo "<br />";
}

}
?>
</code>

You may like to check PHP Tutorial, if you are looking for getting started.

 

Uncategorized

Javascript Regular Expression - Greedy Vs Lazy - Quiz

February 4th, 2012

Take the following quiz for enhancing your understanding of the Regular Expression - Greedy Vs Lazy. You may like to ready the Javascript Regular Expression - Greedy Vs Lazy, before you take up this quiz.

Q1. Consider the following section of the code

var pattern = /^.*([\d]+)/;
var str = "Which year was known as great depression - 1929";
var newstr = str.replace(pattern, "$1");

what is the value of newstr

A. 1929
B. 9
C. Which year was known as great depression - 1929
D. 29

Q2. Consider the following section of code

var pattern = /^.*([\d]*)/;
var str = "Which year was known as great depression - 1929";

What is captured in the parenthesis

A. 1929
B. 9
C. Nothing
D. Which year was known as great depression - 1929

Try to do the above two questions yourself before you check the answers. The answers are at the end of this topic. You may also like to check

1. Javascript Tutorial for beginners

2. Regular Expression In Javascript on amazon

Answers and explanations
Q1. B
We are tempted to think that the parenthesis will capture 1929. However the .* keeps
matching anything including the digits. Finally to make the match successful the last
digit 9 is left to match to [\d]+. This is captured by the parenthesis.
Q2. C
Note that the our greed to attempt to capture 1929 gives a blank result. All the literals
are captured in .* and for the search to be successful, no match is required

Uncategorized

Javascript Regular Expression greedy vs lazy

February 4th, 2012

We will understand the concept of greedy Vs. Lazy regular expression with the help of
an example.

Consider part of an html page consisting of some words in bold. Here is an example

<p> This is an example page </p>
<b> First Bold </b>
Something here
<b> Second Bold </b>
Something else here
<p> Finish </p>
Now, we want to write a regular expression which will match <b> First Bold
</b> in the above example. To do this we use the following regular expression
/<b>.*</b>/
Hoping it to work. This regular expression, however, matches the following part of the
subject
<b> First Bold </b>
Something here
<b> Second Bold </b>
Why ? To understand it we would like to understand the mechanism of match. In the
regular expression /<b>.*</b>/ . The regular expression first looks for <b> in the
subject. Once <b> is found, .* all way to the end of the subject. In the process, it eats
up the whole subject, right all way to the finish </p>. When its stomach is full, it looks
in the regular expression, for what next to match. It is </b>. So what it does is
backtracking.

 

It tries to match < of </b> in the regular expression with > of </p> in the subject. The
match fails. Now it tries matching < of </b> with p of </p>. This also fails. It keeps
doing this till it matches < of </b> in the regular expression with < of </p> in the
subject

<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Javascript Regular Expression Example ch4 Ex 01
Understanding Greedy Vs. Lazy Match
********************************************************
*/
var pattern1=/<b>(.*)<\/b>/;
var string1 = "<p> This is an example page </p><b> First Bold </b>Something here
<b> Second Bold </b> Something else here<p> Finish </p>" ;
var string2 = string1.match(pattern1);
document.write("string2[0] is : ", string2[0] , "<br />");
//-->
</script>
</body>
</html>

 

If we run this code we get the following output

string2[0] is : First Bold Something here Second Bold

Notice that, since we are displaying the output on an html, we do not see <b> and </b>.
We instead see the actual bold letters. But you should be able to feel the idea. The
regular expression /<b>(.*)<\/b>/; matches all the text between the first <b> and all
way to the last </b>.

If we change the statement

var pattern1=/<b>(.*)<\/b>/;
to
var pattern1=/<b>(.*?)<\/b>/;

we get the following output

string2[0] is : First Bold

You may also like to take a small quiz about Regex ( greedy vs lazy)

Also check

1. Javascript Tutorial for beginners

2. Regular Expression In Javascript on amazon

 

 

 

Uncategorized