using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace PhotoEditResize
{
public partial class Form1 : Form
{
Image OriginalImg;
Rectangle CutRec = new Rectangle();
Rectangle CutRec2 = new Rectangle();
bool DrRec = false;
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (DrRec) { e.Graphics.DrawRectangle(Pens.Red, CutRec); }
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
int x = OriginalImg.Width * e.X / pictureBox1.Width;
int y = OriginalImg.Height * e.Y / pictureBox1.Height;
CutRec.X = e.X;
CutRec.Y = e.Y;
CutRec2.X = x;
CutRec2.Y = y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != System.Windows.Forms.MouseButtons.Right) { return; }
DrRec = true;
int x = OriginalImg.Width * e.X / pictureBox1.Width;
int y = OriginalImg.Height * e.Y / pictureBox1.Height;
CutRec.Width = -CutRec.X + e.X;
CutRec.Height = -CutRec.Y + e.Y;
pictureBox2.Invalidate();
pictureBox1.Invalidate();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (!DrRec)
return;
DrRec = false;
int x = OriginalImg.Width * e.X / pictureBox1.Width;
int y = OriginalImg.Height * e.Y / pictureBox1.Height;
CutRec2.Width = x - CutRec2.X; // Ширина
CutRec2.Height = y - CutRec2.Y; // Высота
label5.Text = "Реальные координаты X " + x;
label6.Text = "Реальные координаты Y " + y;
pictureBox1.Refresh();
if ((OriginalImg == null) || (CutRec2.X <= 0 || CutRec2.Y <= 0 || CutRec2.Width <= 0 || CutRec2.Height <= 0))
return;
try
{
var pic = (Bitmap)pictureBox1.Image;
pictureBox2.Image = pic.Clone(CutRec2, PixelFormat.Undefined);
}
catch
{
throw;
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
try { pictureBox1.Image = Image.FromFile(ofd.FileName); OriginalImg = pictureBox1.Image; }
catch (Exception) { }
}
pictureBox1.Invalidate();
}
}
}