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;
using System.Drawing.Imaging;
using System.Reflection;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
string strFilename = "";
OpenFileDialog dlgOpen = null;
try
{
dlgOpen = new OpenFileDialog();
dlgOpen.Filter = "All Image Files(*.*)|*.*";
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
if (dlgOpen.FileName == "") return;
strFilename = dlgOpen.FileName;
pictureBox1.Image = Image.FromFile(strFilename);
this.SetImageSizeMode(pictureBox1);
}
}
catch (Exception ex)
{
MessageBox.Show("无法识别的图片格式(" + strFilename + ")\r\n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (dlgOpen != null)
dlgOpen.Dispose();
dlgOpen = null;
}
}
/********************************************************************************************************/
private void SetImageSizeMode(PictureBox pImage)
{
if (pImage == null) return;
Image imgResult = pImage.Image;
if (imgResult != null)
{
//控制图片显示方式
if (imgResult.Width > pImage.Width || imgResult.Height > pImage.Height)
{
pImage.SizeMode = PictureBoxSizeMode.Zoom;
}
else
{
pImage.SizeMode = PictureBoxSizeMode.CenterImage;
}
}
}
/*****************************************************************************************************/
#region 绘制及剪切
Point fPtStart; //开始位置矩形的位置
bool fHaveImage = false; //是否有图片
Point fPtFirst; //鼠标按下去的位置
Point fPtSecond; //鼠标当前位置

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
this.Cursor = Cursors.Cross;
fPtFirst = new Point(e.X, e.Y);
fPtStart = new Point(e.X, e.Y);
if ((sender as PictureBox).Image != null)
{
this.fHaveImage = true;
}
else
{
this.fHaveImage = false;
}
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (this.Cursor == Cursors.Cross)
{
fPtSecond = new Point(e.X, e.Y);


if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
{
//鼠标当前位置在,按下去位置的右下角
fPtStart = new Point(fPtFirst.X, fPtFirst.Y);
}
else if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) < 0)
{
//鼠标当前位置在,按下去位置的右上角
fPtStart = new Point(fPtFirst.X, fPtSecond.Y);
}
else if ((fPtSecond.X - fPtFirst.X) < 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
{
//鼠标当前位置在,按下去位置的左下角
fPtStart = new Point(fPtSecond.X, fPtFirst.Y);
}
else
{
//鼠标当前位置在,按下去位置的左上角
fPtStart = new Point(fPtSecond.X, fPtSecond.Y);
}
pictureBox1.Invalidate();
}
}


/*******************************************************************************************************/
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (this.fHaveImage == false) return;
try
{
fPtSecond = new Point(e.X, e.Y);
if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
{
//鼠标当前位置在,按下去位置的右下角
fPtStart = new Point(fPtFirst.X, fPtFirst.Y);
}
else if ((fPtSecond.X - fPtFirst.X) > 0 && (fPtSecond.Y - fPtFirst.Y) < 0)
{
//鼠标当前位置在,按下去位置的右上角
fPtStart = new Point(fPtFirst.X, fPtSecond.Y);
}
else if ((fPtSecond.X - fPtFirst.X) < 0 && (fPtSecond.Y - fPtFirst.Y) > 0)
{
//鼠标当前位置在,按下去位置的左下角
fPtStart = new Point(fPtSecond.X, fPtFirst.Y);
}
else
{
//鼠标当前位置在,按下去位置的左上角
fPtStart = new Point(fPtSecond.X, fPtSecond.Y);
}

Rectangle rectDisplay = this.GetPictureDisplaySize(pictureBox1); //图片实际显示的大小


int Width = Math.Abs((fPtSecond.X - fPtFirst.X));
int Height = Math.Abs((fPtSecond.Y - fPtFirst.Y));

double dWRate = pictureBox1.Image.Width / (double)rectDisplay.Width; //缩放比例:宽
double dHRate = pictureBox1.Image.Height / (double)rectDisplay.Height; //缩放比例:高

int intRealWidth = (int)(dWRate * Width); //实际需要截取的图片宽度
int intRealHeight = (int)(dHRate * Height); //实际需要截取的图片高度

int intRealX = (int)((fPtStart.X - rectDisplay.X) * dWRate); //实际的X坐标
int intRealY = (int)((fPtStart.Y - rectDisplay.Y) * dHRate); //实际的Y坐标

Bitmap bmpDest = new Bitmap(intRealWidth, intRealHeight, PixelFormat.Format32bppRgb); //目标图片大小

Graphics g = Graphics.FromImage(bmpDest); //创建GDI

Rectangle rectDest = new Rectangle(0, 0, intRealWidth, intRealHeight);
Rectangle rectSource = new Rectangle(intRealX, intRealY, intRealWidth, intRealHeight);

g.DrawImage(pictureBox1.Image, rectDest, rectSource, GraphicsUnit.Pixel); //绘图

pictureBox2.Image = (Image)bmpDest;

g.Dispose();

SetImageSizeMode(pictureBox2);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{ this.Cursor = Cursors.Default; }
}

private Rectangle GetPictureDisplaySize(PictureBox pbxImage)
{
if (pbxImage != null)
{
PropertyInfo ppiImageRect = pbxImage.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
return (Rectangle)ppiImageRect.GetValue(pbxImage, null);
}
return new Rectangle(0, 0, 1, 1);
}
/***********************************************************************************/
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
int intWidth = 0;
int intHeight = 0;
if (this.fHaveImage == true)
{
Pen p = new Pen(Color.Black, 1);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

intWidth = Math.Abs(fPtFirst.X - fPtSecond.X);
intHeight = Math.Abs(fPtFirst.Y - fPtSecond.Y);

Rectangle rectDraw = new Rectangle(fPtStart, new Size(intWidth, intHeight));
e.Graphics.DrawRectangle(p, rectDraw);
}
}


/***************************************************************************************************/
#endregion
/// <summary>
/// 保存剪切后的图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog dlgSave = new SaveFileDialog();

if (dlgSave.ShowDialog() == DialogResult.OK)
{
pictureBox2.Image.Save(dlgSave.FileName);
}
}

private void pictureBox2_Click(object sender, EventArgs e)
{

}


}
}