المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Draw resizeable and draggable rectangle on picturebox to crop image



C# Programming
12-07-2009, 07:02 AM
Hi everyone!
I have an app: draw a resizeable and draggable rectangle on form.
Now i want to modify it to draw a resizeable and draggable rectangle on picturebox not on Form to select area and crop image.
I tried a few ways but i can't. I'm newbie in C#. Please help me! Thank you so much!
Here's my code:


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace DragResizeRectangle
{
///
/// Description of MainForm.
///
public partial class MainForm : Form
{
private RectangleF DragRect=new RectangleF(20, 20, 100, 80);

private System.Drawing.RectangleF[] Adornments = new RectangleF[0];

private bool MouseInRect = false;
private bool RectDragging = false;

private bool RectResizing = false;

private int ActiveAdornment = -1;

private Point MousePos;
private SolidBrush BackBrush;

private System.Drawing.Pen[] LinePen = new Pen[0];

public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

void MainFormLoad(object sender, EventArgs e)
{
Adornments=new RectangleF[8];
LinePen=new Pen[2];

BackBrush = new SolidBrush(BackColor);
LinePen[0] = new Pen(ForeColor);
LinePen[1] = new Pen(ForeColor);
LinePen[1].DashPattern = new float[] { 5, 3 };

CalcAdornments();

SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);

}
private void CalcAdornments()
{
Adornments[0] = new RectangleF(DragRect.X - 4, DragRect.Y - 4, 8, 8);
Adornments[1] = new RectangleF(DragRect.X + DragRect.Width / 2 - 4, DragRect.Y - 4, 8, 8);
Adornments[2] = new RectangleF(DragRect.X + DragRect.Width - 4, DragRect.Y - 4, 8, 8);
Adornments[3] = new RectangleF(DragRect.X + DragRect.Width - 4, DragRect.Y + DragRect.Height / 2 - 4, 8, 8);
Adornments[4] = new RectangleF(DragRect.X + DragRect.Width - 4, DragRect.Y + DragRect.Height - 4, 8, 8);
Adornments[5] = new RectangleF(DragRect.X + DragRect.Width / 2 - 4, DragRect.Y + DragRect.Height - 4, 8, 8);
Adornments[6] = new RectangleF(DragRect.X - 4, DragRect.Y + DragRect.Height - 4, 8, 8);
Adornments[7] = new RectangleF(DragRect.X - 4, DragRect.Y + DragRect.Height / 2 - 4, 8, 8);
}

private void SetAdornmentCursor()
{
if (ActiveAdornment != -1) {
switch (ActiveAdornment) {
case 0:
Cursor = Cursors.SizeNWSE;
break;
case 1:
Cursor = Cursors.SizeNS;
break;
case 2:
Cursor = Cursors.SizeNESW;
break;
case 3:
Cursor = Cursors.SizeWE;
break;
case 4:
Cursor = Cursors.SizeNWSE;
break;
case 5:
Cursor = Cursors.SizeNS;
break;
case 6:
Cursor = Cursors.SizeNESW;
break;
case 7:
Cursor = Cursors.SizeWE;
break;
}
}
else {
Cursor = Cursors.Default;
}
}

void MainFormMouseDown(object sender, MouseEventArgs e)
{

if (e.Button == MouseButtons.Left) {
if (MouseInRect & ActiveAdornment == -1) {
RectDragging = true;
MousePos = new Point(e.X, e.Y);
MousePos = PointToScreen(MousePos);
}
else if (ActiveAdornment != -1) {
RectResizing = true;
MousePos = new Point(e.X, e.Y);
MousePos = PointToScreen(MousePos);
}
}

}

void MainFormMouseMove(object sender, MouseEventArgs e)
{

if (e.Button == MouseButtons.Left) {
Point CurrMouse = new Point(e.X, e.Y);

CurrMouse.X = Math.Max(0, Math.Min(CurrMouse.X, ClientSize.Width));
CurrMouse.Y = Math.Max(0, Math.Min(CurrMouse.Y, ClientSize.Height));

CurrMouse = PointToScreen(CurrMouse);

if (RectDragging) {
if (Math.Abs(CurrMouse.X - MousePos.X) > 2 | Math.Abs(CurrMouse.Y - MousePos.Y) > 2) {
DragRect.Offset(CurrMouse.X - MousePos.X, CurrMouse.Y - MousePos.Y);

DragRect.X = Math.Max(0, Math.Min(DragRect.X, ClientSize.Width - DragRect.Width));
DragRect.Y = Math.Max(0, Math.Min(DragRect.Y, ClientSize.Height - DragRect.Height));

MousePos = new Point(e.X, e.Y);

MousePos.X = Math.Max(0, Math.Min(MousePos.X, ClientSize.Width));
MousePos.Y = Math.Max(0, Math.Min(MousePos.Y, ClientSize.Height));

MousePos = PointToScreen(MousePos);
CalcAdornments();
Invalidate();
}
}
else if (RectResizing) {
if (ActiveAdornment != -1) {

if (Math.Abs(CurrMouse.X - MousePos.X) > 2 | Math.Abs(CurrMouse.Y - MousePos.Y) > 2) {
switch (ActiveAdornment) {
case 0:
DragRect.Height -= CurrMouse.Y - MousePos.Y;
if (!(DragRect.Height < 20)) {
DragRect.Y -= -CurrMouse.Y + MousePos.Y;
}
else {
DragRect.Height = 20;
}


DragRect.Width -= CurrMouse.X - MousePos.X;
if (!(DragRect.Width < 20)) {
DragRect.X -= -CurrMouse.X + MousePos.X;
}
else {
DragRect.Width = 20;
}

break;
case 1:
DragRect.Height -= CurrMouse.Y - MousePos.Y;
if (!(DragRect.Height < 20)) {
DragRect.Y -= -CurrMouse.Y + MousePos.Y;
}
else {
DragRect.Height = 20;
}

break;
case 2:
DragRect.Height -= CurrMouse.Y - MousePos.Y;
if (!(DragRect.Height < 20)) {
DragRect.Y -= -CurrMouse.Y + MousePos.Y;
}
else {
DragRect.Height = 20;
}


DragRect.Width += CurrMouse.X - MousePos.X;
DragRect.Width = Math.Max(20, DragRect.Width);
break;
case 3:
DragRect.Width += CurrMouse.X - MousePos.X;
DragRect.Width = Math.Max(20, DragRect.Width);
break;
case 4:
DragRect.Width += CurrMouse.X - MousePos.X;
DragRect.Width = Math.Max(20, DragRect.Width);

DragRect.Height += CurrMouse.Y - MousePos.Y;
DragRect.Height = Math.Max(20, DragRect.Height);
break;
case 5:
DragRect.Height += CurrMouse.Y - MousePos.Y;
DragRect.Height = Math.Max(20, DragRect.Height);
break;
case 6:
DragRect.Height += CurrMouse.Y - MousePos.Y;
DragRect.Height = Math.Max(20, DragRect.Height);

DragRect.Width -= CurrMouse.X - MousePos.X;
if (!(DragRect.Width < 20)) {
DragRect.X -= -CurrMouse.X + MousePos.X;
}
else {
DragRect.Width = 20;
}

break;
case 7:
DragRect.Width -= CurrMouse.X - MousePos.X;
if (!(DragRect.Width < 20)) {
DragRect.X -= -CurrMouse.X + MousePos.X;
}
else {
DragRect.Width = 20;
}

break;
}

MousePos = new Point(e.X, e.Y);

MousePos.X = Math.Max(0, Math.Min(MousePos.X, ClientSize.Width));
MousePos.Y = Math.Max(0, Math.Min(MousePos.Y, ClientSize.Height));

MousePos = PointToScreen(MousePos);
CalcAdornments();
Invalidate();
}
}
}
}
else {
MouseInRect = false;
ActiveAdornment = -1;

RectangleF tmprect = default(RectangleF);
int i = 0;
foreach (RectangleF tmprect_loopVariable in Adornments) {
tmprect = tmprect_loopVariable;
if (tmprect.Contains(e.X, e.Y)) {
MouseInRect = true;
ActiveAdornment = i;
SetAdornmentCursor();
Invalidate();
return;
}
i += 1;
}

if (DragRect.Contains(e.X, e.Y)) {
MouseInRect = true;
Cursor = Cursors.SizeAll;
Invalidate();
return;
}

Cursor = Cursors.Default;
Invalidate();
}

}

void MainFormMouseUp(object sender, MouseEventArgs e)
{

if (e.Button == MouseButtons.Left) {
RectDragging = false;
RectResizing = false;
Invalidate();
}

}

void MainFormPaint(object sender, PaintEventArgs e)
{

Graphics g = e.Graphics;


g.FillRectangle(BackBrush, g.ClipBounds);

if (!MouseInRect) {
g.DrawRectangle(LinePen[0], DragRect.X, DragRect.Y, DragRect.Width, DragRect.Height);
}
else {
g.DrawRectangle(LinePen[1], DragRect.X, DragRect.Y, DragRect.Width, DragRect.Height);
g.FillRectangles(Brushes.White, Adornments);
g.DrawRectangles(Pens.Black, Adornments);
}
}
}
}