Please help me it`s don`t work Correct Crop picturebox

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Imaging.Filters;



namespace Foto Crop
{
public partial class Kadrowanie : Form
{


private Bitmap _sourceImage;
private Bitmap _croppedImage;
private Rectangle _rect;

private PointF _firstPoint;
private PointF _secondPoint;
private bool _isSecondPoint;
private bool _isPanning;
private Point _mouseDownLocation;
private Size cmbSizeMode;

private float _zoom = 1.0F;
public float _offset = 0.0F;
private double _startX = 0.0;
private double _startY = 0.0;
private bool _dontReset = false;



public Kadrowanie()
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(Kadrowanie_MouseWheel);
// pobieranie zdjęcia z poprzedniej formy

/*if (Clipboard.ContainsImage())
{
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox2.Image = Clipboard.GetImage();

} */
}

private void Kadrowanie_MouseWheel(object sender, MouseEventArgs e)
{
// zooming in and out
float num = 1.0001f;
if (e.Delta < 0)
num = -0.1f;
pictureBox2.Width += Convert.ToInt32(pictureBox2.Image.Width * num);
pictureBox2.Height += Convert.ToInt32(pictureBox2.Image.Height * num);
pictureBox2.Refresh();

this.CalcZoom();
}

/* private void btnLoadImage_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image = this._sourceImage = (Bitmap)Image.FromFile(this.openFileDialog1.FileName);
}
}

private void btnSaveImage_Click(object sender, EventArgs e)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
_croppedImage.Save(this.saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
*/
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
RectangleF r = new RectangleF(_firstPoint.X, _firstPoint.Y, _secondPoint.X - _firstPoint.X, _secondPoint.Y - _firstPoint.Y);
if (_isSecondPoint || (r.Width > 0 && r.Height > 0))
{
e.Graphics.DrawRectangle(Pens.Red, _firstPoint.X, _firstPoint.Y,
_secondPoint.X - _firstPoint.X, _secondPoint.Y - _firstPoint.Y);
}
}

private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
_mouseDownLocation = e.Location;
}

private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (this._isPanning)
{
Point mousePosNow = e.Location;

int deltaX = mousePosNow.X - _mouseDownLocation.X;
int deltaY = mousePosNow.Y - _mouseDownLocation.Y;

int newX = pictureBox2.Location.X + deltaX;
int newY = pictureBox2.Location.Y + deltaY;

pictureBox2.Location = new Point(newX, newY);

//_firstPoint.X += deltaX;
//_firstPoint.Y += deltaY;

//if (_isSecondPoint)
//{
// _secondPoint.X += deltaX;
// _secondPoint.Y += deltaY;
//}
}
else if (!_isSecondPoint)
{
_firstPoint.X = e.X;
_firstPoint.Y = e.Y;
_isSecondPoint = true;
}
else
{
_secondPoint.X = e.X;
_secondPoint.Y = e.Y;
}
pictureBox2.Invalidate();
}
else
{
if (_isSecondPoint)
{
_isSecondPoint = false;

if (this._sourceImage.Width > this._sourceImage.Height)
{
_firstPoint.Y -= _offset;
_secondPoint.Y -= _offset;
}
else
{
_firstPoint.X -= _offset;
_secondPoint.X -= _offset;
}

// crop image size
_firstPoint.X /= _zoom;
_firstPoint.Y /= _zoom;
_secondPoint.X /= _zoom;
_secondPoint.Y /= _zoom;

_rect.X = (int)(_startX + _firstPoint.X);
_rect.Y = (int)(_startY + _firstPoint.Y);
_rect.Width = (int)(_secondPoint.X - _firstPoint.X);
_rect.Height = (int)(_secondPoint.Y - _firstPoint.Y);
}
}
}


private void ClearRectangle()
{
_rect = Rectangle.Empty;
pictureBox2.Invalidate();
}

public Image Crop(Image image, Rectangle selection)
{
Bitmap bmp = image as Bitmap;

// Check if it is a bitmap:
if (bmp == null)
throw new ArgumentException("No valid bitmap");

// Crop the image:
Bitmap cropBmp = null;

try
{
if (selection.X >= 0 && selection.Y >= 0 && selection.X + selection.Width < this._sourceImage.Width && selection.Y + selection.Height < this._sourceImage.Height)
cropBmp = bmp.Clone(selection, bmp.PixelFormat);
else
{
//do something
}
}
catch
{
if (cropBmp != null)
cropBmp.Dispose();
}

// Release the resources:
image.Dispose();

return cropBmp;
}

/* private object StringToEnum(Type t, string Value)
{
foreach (FieldInfo fi in t.GetFields())
if (fi.Name == Value)
return fi.GetValue(null); // We use null because
// enumeration values
// are static

throw new Exception(string.Format("Cant convert {0} to {1}", Value, t.ToString()));
}
*/

/* private void btnPan_Click(object sender, EventArgs e)
{
if (this._isPanning)
{
this._isPanning = this.btnPan.Checked = false;
}
else
{
this._isPanning = this.btnPan.Checked = true;
}
}
*/

private void cmbSizeMode_SelectedIndexChanged(object sender, EventArgs e)
{
/* string selectedItem = this.cmbSizeMode.SelectedItem.ToString();
this.pictureBox2.SizeMode = (PictureBoxSizeMode)this.StringToEnum(typeof(PictureBoxSizeMode), selectedItem);
*/

this._zoom = 1.0F;
_offset = 0.0F;

//could be done in a switch
if (this.pictureBox2.SizeMode == PictureBoxSizeMode.Zoom)
{
_startX = 0.0;
_startY = 0.0;

CalcZoom();
}
if (this.pictureBox2.SizeMode == PictureBoxSizeMode.StretchImage)
{
_startX = 0.0;
_startY = 0.0;

CalcZoomStretch();
}
if (this.pictureBox2.SizeMode == PictureBoxSizeMode.Normal)
{
_startX = 0.0;
_startY = 0.0;
}

if (this.pictureBox2.SizeMode == PictureBoxSizeMode.CenterImage)
{
_startX = ((double)this.pictureBox2.Image.Width - (double)this.pictureBox2.ClientSize.Width) / 2.0;
_startY = ((double)this.pictureBox2.Image.Height - (double)this.pictureBox2.ClientSize.Height) / 2.0;
}
if (this.pictureBox2.SizeMode == PictureBoxSizeMode.AutoSize)
{
_startX = 0.0;
_startY = 0.0;
}

//reset points
if (!_dontReset)
{
this._firstPoint.X = this._secondPoint.X = this._firstPoint.Y = this._secondPoint.Y = 0;
this.pictureBox2.Invalidate();
}

_dontReset = false;
}

private void CalcZoomStretch()
{
throw new NotImplementedException();
}

private void CalcZoom()
{
double factor1 = (double)this._sourceImage.Width / (double)this._sourceImage.Height;
double factor2 = (double)this.pictureBox2.Width / (double)this.pictureBox2.Height;

if (factor1 > factor2)
{
_zoom = (float)((double)this.pictureBox2.Width / (double)this._sourceImage.Width);
}
else
{
_zoom = (float)((double)this.pictureBox2.Height / (double)this._sourceImage.Height);
}

if (_zoom != 1.0F)
{
if (this._sourceImage.Width > this._sourceImage.Height)
_offset = (float)(this.pictureBox2.Height - (this._sourceImage.Height * _zoom)) / 2.0F;
else
_offset = (float)(this.pictureBox2.Width - (this._sourceImage.Width * _zoom)) / 2.0F;
}
else
_offset = 0.0F;
}

/* private void Kadrowanie_Load(object sender, EventArgs e)
{
this.cmbSizeMode.SelectedIndex = 0;
}
*/
private void pictureBox2_SizeChanged(object sender, EventArgs e)
{
_dontReset = true;
double oldX = _startX;
double oldY = _startY;

cmbSizeMode_SelectedIndexChanged(this.cmbSizeMode, new EventArgs());

_firstPoint.X += (float)(oldX - _startX);
_firstPoint.Y += (float)(oldY - _startY);
_secondPoint.X += (float)(oldX - _startX);
_secondPoint.Y += (float)(oldY - _startY);

this.pictureBox2.Invalidate();
}

private void button1_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(this._sourceImage);

this._croppedImage = this.Crop(bmp, _rect) as Bitmap;
pictureBox2.Image = this._croppedImage;


this.ClearRectangle();

//reset point and picbox, else picbox could go outn of sight
this._firstPoint.X = this._secondPoint.X = this._firstPoint.Y = this._secondPoint.Y = 0;
//this.pictureBox2.Location = new Point(0, 0);
}

private void button2_Click(object sender, EventArgs e)
{
pictureBox2.Image = _sourceImage;
}

private void button3_Click(object sender, EventArgs e)
{
// Zoom In
pictureBox2.Width += Convert.ToInt32(pictureBox2.Image.Width * 1.2);
pictureBox2.Height += Convert.ToInt32(pictureBox2.Image.Height * 1.2);
pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;

//pictureBox1.Top = (this.ClientSize.Height - 100 - pictureBox1.Height) / 2;
//pictureBox1.Left = (this.ClientSize.Width - 20 - pictureBox1.Width) / 2;
pictureBox2.Refresh();
}

private void button4_Click(object sender, EventArgs e)
{
this.pictureBox2.Image.RotateFlip(RotateFlipType.Rotate90FlipXY);
this.pictureBox2.Refresh();

this._croppedImage = (Bitmap)this.pictureBox2.Image;
}

private void button5_Click(object sender, EventArgs e)
{
this.pictureBox2.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
this.pictureBox2.Refresh();

this._croppedImage = (Bitmap)this.pictureBox2.Image;
}

private void button6_Click(object sender, EventArgs e)
{
OpenFileDialog do_kadrowania = new OpenFileDialog();
do_kadrowania.Filter = "Pliki graficzne |*.jpg; *.tiff; *.raw";
if (do_kadrowania.ShowDialog() == DialogResult.OK)
{
this.pictureBox2.Image = this._sourceImage = (Bitmap)Image.FromFile(do_kadrowania.FileName);
}
}

private void button7_Click(object sender, EventArgs e)
{

SaveFileDialog wykadrowane = new SaveFileDialog();
wykadrowane.Filter = "Pliki graficzne |*.jpg; *.tiff; *.raw";
if (wykadrowane.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("Zapisanie zdjęcia zostało anulowane!", "UWAGA!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
_croppedImage.Save(wykadrowane.FileName);//saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
//pictureBox2.Image.Save(wykadrowane.FileName);
MessageBox.Show("Zapisałeś/łaś plik: " + wykadrowane.FileName);
}


}

private void button8_Click(object sender, EventArgs e)
{
// Zoom Out
pictureBox2.Top = (this.ClientSize.Height - 100 - pictureBox2.Height) / 2;
pictureBox2.Left = (this.ClientSize.Width - 20 - pictureBox2.Width) / 2;

pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox2.Refresh();
}
}

}
1.This Program select Area image to Crop, This function Crop and zooming image from cloud :( This Crop not selected from in Rectangle Location :(
Don`t View select Area to crop, are You Help ?
What is not correctly ?
2. This function Zoom out & Zoom In Move picturebox to button area :(
Please Help How to zoom in & zoom out set to only picturebox ? :(

View the full article
 
Back
Top