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
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 thisusing 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();
}
}
}