Windows Presentation Foundation.

I think all of you getting pretty confusion why need to explain about WPF cause of lot explanation available in more pages. Actually I am going to post some Image manipulation functionalism in upcoming posts. So here is the introduction to WPF.

The Windows Presentation Foundation is a Graphical subsystem in .Net Framework 3.0 which uses a markup language, known as XAML for rich user interface development. WPF formerly code named Avalon. WCF – Indigo, CardSpace – InfoCard, WF – WorkFlow. WPF is included with Windows Vista, Windows Server 2008, Windows XP Service Pack 2 or later and Windows Server 2003.

It provides a clear separation between the user interface and the business logic. A WPF application can be deployed on the desktop or hosted in a web browser. So we can create a wide range of both standalone and browser-hosted applications.

It aims to unify a number of application services: user interface, 2D and 3D drawing, fixed and adaptive documents, advanced typography, vector graphics, raster graphics, animation, data binding, audio and video.

Some examples are Yahoo! Messenger and the New York Times Reader, as well as the Contoso Healthcare Sample Application. WPF is included in the Microsoft .NET Framework, so you can build applications that incorporate other elements of the .NET Framework class library.

I hope this will make you some ideas in WPF. Let we stop the introduction of WPF from here.


Image Resize using C#.

Use the following method to resize an image.

public Image ResizeImage(Image image, int maxWidth, int maxHeight)
if (image == null)
return image;
int width = image.Width;
int height = image.Height;

double widthFactor = (Convert.ToDouble(width) / Convert.ToDouble(maxWidth));
double heightFactor = (Convert.ToDouble(height) / Convert.ToDouble(maxHeight));

if (widthFactor <>
// Skip resize
return image;
int newWidth;
int newHeight;
if (widthFactor > heightFactor)
newWidth = Convert.ToInt32(Convert.ToDouble(width) / widthFactor);
newHeight = Convert.ToInt32(Convert.ToDouble(height) / widthFactor);
newWidth = Convert.ToInt32(Convert.ToDouble(width) / heightFactor);
newHeight = Convert.ToInt32(Convert.ToDouble(height) / heightFactor);
if (newHeight == 0)
newHeight = 1;
if (newWidth == 0)
newWidth = 1;
Bitmap bitmap = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bitmap.SetResolution(96, 96);

Graphics graphics = Graphics.FromImage(bitmap);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
return bitmap;


Bytes to GB MB Conversion using C#

Hi all,

Just copy the following method to getting file size with GB or MB formatted.

private string GetFileSize(long Bytes)
if (Bytes >= 1073741824)
Decimal size = Decimal.Divide(Bytes, 1073741824);
return String.Format("{0:##.##} GB", size);
else if (Bytes >= 1048576)
{ Decimal size = Decimal.Divide(Bytes, 1048576);
return String.Format("{0:##.##} MB", size);
else if (Bytes >= 1024)
{ Decimal size = Decimal.Divide(Bytes, 1024);
return String.Format("{0:##.##} KB", size);
else if (Bytes > 0 & Bytes <>
{ Decimal size = Bytes;
return String.Format("{0:##.##} Bytes", size);
return "0 Bytes";


Cropping Image

Use the following code to crop an image by C#.

public Image CropImage(Image img, int cropLeft, int cropTop, int cropWidth, int cropHeight)
Bitmap bmp = new Bitmap(img);
Rectangle rect = new Rectangle(cropLeft, cropTop, cropWidth, cropHeight);
Bitmap croppedBmp = bmp.Clone(rect, bmp.PixelFormat);
return croppedBmp;


Cropping an Image with best quality using C#

Hi all,

Here is the sample code for getting cropped image with the best quality. Try it out.

public Image CropImage(Image img, int left, int top, int width, int height)



Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);

bmp.SetResolution(80, 60);

Graphics graphics = Graphics.FromImage(bmp);

graphics.SmoothingMode = SmoothingMode.AntiAlias;

graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

graphics.DrawImage(img, new Rectangle(0, 0, width, height), left, top, width, height, GraphicsUnit.Pixel);




return bmp;

catch (Exception ex)



return null;


that's it....


Query for getting number of employee by department wise.

Hi all,

Here I am having the following two tables.

Table : Department

Id Name

--- ----

1 IT

2 Maths

3 Science

Table: Employee

Id Name DeptId

--- ---- ------

1 aaa 2

2 vvv 2

3 eee 2

4 www 1

5 yyy 2

6 ppp 3

7 ooo 1

8 rrr 3

9 uuu 1

10 iii 3

Now, I need to get the number of employee in each department. How to do?

Here is the query for that.

select dept.Name as DepartmentName,count(*) as NoOfEmp from Department dept

INNER JOIN Employee emp on emp.DeptId=dept.Id group by dept.Name

By running this query, we can get the values like as follows.

DepartmentName NoOfEmp

-------------- -------

IT 3

Maths 4

Science 3

That's it...


Getting the GridViewRow from checkbox checked event.

Get the GridView Row when a checkbox clicked from a GridView.

Here I’m going to give a sample code for getting the selected GridView Row from a Gridview when a checkbox clicked from that gridview.

protected void chkSelect_CheckedChanged(object sender, EventArgs e)


CheckBox selectChkBox = (CheckBox)sender;

GridViewRow row = (GridViewRow)( selectChkBox.Parent.Parent);

int index = row.RowIndex;


Here the “index” is the row of GridView.