Sunday 15 July 2012

How to Crop image using C#.Net Windows Application

Introduction:

In this article I will explain about how to crop image/photo using C#.Net Windows Application.

Description:

I have working with one of the application, i got the requirements like Cropping image/photo passport size

First we need to display image in windows form using pictureBox control .here we have added 2 pictureBox controls ie pictureBox1=picOriginal and pictureBox2=picCropped. The Desing window as shown in bellow

 Set the pictureBox1 properties:
Add the image which image u want to crop
pictureBox1 events Click : MouseUp,MouseMove,MouseDown,Paint

C#.Net Code Snippet: 

Now open the code behind the file and add the following code. 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Usage_CsharpControls
{
    public partial class CropImage : Form
    {
        //create variables for cropping rectanglesize ie x-axis, y-axis, width and height
        int cropX, cropY, cropWidth, cropHeight;

        public CropImage()
        {
            InitializeComponent();
        }

        private void picOrigional_MouseUp(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Default;
            if (cropWidth < 1)
            {
                return;
            }
            Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
            Bitmap bit = new Bitmap(picOriginal.Image, picOriginal.Width, picOriginal.Height);
            Bitmap crop = new Bitmap(cropWidth, cropHeight);
            Graphics gfx = Graphics.FromImage(crop);
            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;//here add  System.Drawing.Drawing2D namespace;
            gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;//here add  System.Drawing.Drawing2D namespace;
            gfx.CompositingQuality = CompositingQuality.HighQuality;//here add  System.Drawing.Drawing2D namespace;
            gfx.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel);
            picCropped.Image = crop;
            picOriginal.Refresh();
        }

        private void picOrigional_MouseMove(object sender, MouseEventArgs e)
        {
            if (picOriginal.Image == null)
                return;

            if (e.Button == MouseButtons.Left)//here i have use mouse click left button only
            {
                picOriginal.Refresh();
                cropWidth = e.X - cropX;
                cropHeight = e.Y - cropY;
            }
            picOriginal.Refresh();
        }
        //here rectangle border pen color=red and size=2;
        Pen borderpen = new Pen(Color.Red, 2);
        //fill the rectangle color =white
        SolidBrush rectbrush = new SolidBrush(Color.FromArgb(100, Color.White));

        private void picOrigional_Paint(object sender, PaintEventArgs e)
        {
            Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
            Graphics gfx = e.Graphics;
            gfx.DrawRectangle(borderpen, rect);
            gfx.FillRectangle(rectbrush, rect);
        }

        private void picOrigional_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)//here i have use mouse click left button only
            {
                picOriginal.Refresh();
                cropX = e.X;
                cropY = e.Y;
                Cursor = Cursors.Cross;
            }
            picOriginal.Refresh();
        }
    }
}
If you run the application the output look like this



Step by step Installation of SQL Server 2008 in Windows 7 very Easy

Introduction:

In this article i will explain about installation of SQL Server 2008 in Windows 7 very easy  with Snapshot.
Step1: Open  your SQL Server 2008 Drive and folders. After what my system looks as shown in figure.


Step2: Double click the Setup.exe file. After a few seconds the two windows would come up at the a time. The bellow window repeated 2 or 3 time.

Step3: If you checkout the online compatibility click Check for solution online otherwise click Run Program. After a few seconds the bellow dialog box appear.
Step4: The above window will disappear and then the main installation window appears. 

Step5:In the left selection of the above window, Click Installation hyperlink.

Step6:In the right selection, Click New SQL Server stand-alone Installation. The installation would starts please wait few seconds the dialog box appear.

 
Step7: The dialog box disappear, another window would come up.

 Step8: The complete the progress click Ok. the window would come up.

Step9: Click on the Install 
 
 Step10:(Optional) some times the above window displayed. Go to Step5 doing the same steps.

 Step11: Click on the Next button

Step12: Installation type as your wish i.e  here we can Click first radio button Perform New installation of SQL Server 2008 

Step13: If you install free edition click first radio button or if you are install with product key, click second radio button and enter the product key. Then click Next

 Step14:The License Terms page, Select the I accept the license terms check box and then click Next
 
Step15: Feature Selection: Select the features which you want to install. Here we can click Select all button and click Next button again

Step16:Instance Configuration:  Most of the cases, you should accept the Default instance as MSSQLSERVER.

Step17:Disk Space Requirements: If you have a sufficient disk space on the drive and Installation pathe where you want to install. click Next 

Step18: Server Configuration: here carefully observe the server accounts. click on the Select the same account for all sql server services  otherwise you use different server accounts.

 Step19: Enter the account name and password or browse the network server accounts.

 Step20:Check out the user name and password and click Next

Step21:Database Engine Configuration: Select the authentication mode which you want and Click Add Current User and click Next again

 Step22:Analysis Services Configuration: Click on the Add Current user and click Next

Step23:Reporting Services Configuration: select the last Check box Install, but do not configure the report server.

Steps24: Select Check boxes if you want to help Microsoft help you.Click on Next again 

Step25: Read it and Click Next 

Step26: To start installation, Click Install. The progress bar indicates the evolution.

Step27:This can take while completion of installation after clcik Next. 
Step28: After Next button read the message and click Close