Please Subscribe my Youtube Channel
Video tutorial for crop tool https://youtu.be/7IR6J8Kw8cE
9
Video tutorial for crop tool https://youtu.be/7IR6J8Kw8cE
9
Here is the source code for program of image crop in C sharp.➠➡➧➤
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace crop_tool_pro
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Load image
Bitmap bmp = new Bitmap("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg");
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = (Image)bmp;
}
private void button1_Click(object sender, EventArgs e)
{
//here we declare mouse event handlers
pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
pictureBox1.MouseEnter += new EventHandler(pictureBox1_MouseEnter);
Controls.Add(pictureBox1);
}
//declare some variable for crop coordinates
int crpX,crpY,rectW,rectH;
// Declare crop pen for cropping image
public Pen crpPen = new Pen(Color.White);
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Cursor = Cursors.Cross;
crpPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
// set initial x,y co ordinates for crop rectangle
//this is where we firstly click on image
crpX = e.X;
crpY = e.Y;
}
}
private void button2_Click(object sender, EventArgs e)
{
label2.Text = "Dimensions :" + rectW + "," + rectH;
Cursor = Cursors.Default;
//Now we will draw the cropped image into pictureBox2
Bitmap bmp2 = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bmp2, pictureBox1.ClientRectangle);
Bitmap crpImg = new Bitmap(rectW, rectH);
for( int i = 0; i < rectW; i++)
{
for(int y = 0; y < rectH; y++)
{
Color pxlclr = bmp2.GetPixel(crpX + i, crpY + y);
crpImg.SetPixel(i, y, pxlclr);
}
}
pictureBox2.Image = (Image)crpImg;
pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
base.OnMouseEnter(e);
Cursor = Cursors.Cross;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Refresh();
//set width and height for crop rectangle.
rectW = e.X - crpX;
rectH = e.Y - crpY;
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(crpPen, crpX, crpY, rectW, rectH);
g.Dispose();
}
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
Cursor = Cursors.Default;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace crop_tool_pro
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Load image
Bitmap bmp = new Bitmap("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg");
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = (Image)bmp;
}
private void button1_Click(object sender, EventArgs e)
{
//here we declare mouse event handlers
pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
pictureBox1.MouseEnter += new EventHandler(pictureBox1_MouseEnter);
Controls.Add(pictureBox1);
}
//declare some variable for crop coordinates
int crpX,crpY,rectW,rectH;
// Declare crop pen for cropping image
public Pen crpPen = new Pen(Color.White);
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Cursor = Cursors.Cross;
crpPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
// set initial x,y co ordinates for crop rectangle
//this is where we firstly click on image
crpX = e.X;
crpY = e.Y;
}
}
private void button2_Click(object sender, EventArgs e)
{
label2.Text = "Dimensions :" + rectW + "," + rectH;
Cursor = Cursors.Default;
//Now we will draw the cropped image into pictureBox2
Bitmap bmp2 = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bmp2, pictureBox1.ClientRectangle);
Bitmap crpImg = new Bitmap(rectW, rectH);
for( int i = 0; i < rectW; i++)
{
for(int y = 0; y < rectH; y++)
{
Color pxlclr = bmp2.GetPixel(crpX + i, crpY + y);
crpImg.SetPixel(i, y, pxlclr);
}
}
pictureBox2.Image = (Image)crpImg;
pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
base.OnMouseEnter(e);
Cursor = Cursors.Cross;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Refresh();
//set width and height for crop rectangle.
rectW = e.X - crpX;
rectH = e.Y - crpY;
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(crpPen, crpX, crpY, rectW, rectH);
g.Dispose();
}
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
Cursor = Cursors.Default;
}
}
}
