111,097
社区成员




public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.MouseDown+=new MouseEventHandler(pictureBox1_MouseDown);
pictureBox1.MouseMove+=new MouseEventHandler(pictureBox1_MouseMove);
pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
}
private Rectangle m_MouseRect = Rectangle.Empty;
public delegate void SelectRectangel(object sneder, Rectangle e);
public event SelectRectangel SetRectangel;
private bool m_MouseIsDown = false;
int intwidth = 0;
int intheigh = 0;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
m_MouseIsDown = true;
DrawStart(new Point(e.X, e.Y));
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (m_MouseIsDown) ResizeToRectangle(new Point(e.X, e.Y));
}
float w = 0;//宽度比例
float h = 0;//高度比例
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
w = (float)intwidth / (float)pictureBox1.Width;
h = (float)intheigh / (float)pictureBox1.Height;
Cursor.Clip = Rectangle.Empty;
m_MouseIsDown = false;
DrawRectangle();
if (m_MouseRect.X == 0 || m_MouseRect.Y == 0 || m_MouseRect.Width == 0 || m_MouseRect.Height == 0)
{
//如果区域没0 就不执行委托
}
else
{
if (SetRectangel != null) SetRectangel(pictureBox1, m_MouseRect);
}
DrawRectangle();
//ControlPaint.DrawReversibleFrame(pictureBox1.RectangleToScreen(m_MouseRect), Color.Red, FrameStyle.Thick);
}
/// <summary>
/// 刷新绘制
/// </summary>
/// <param name="p"></param>
private void ResizeToRectangle(Point p_Point)
{
DrawRectangle();
m_MouseRect.Width = p_Point.X - m_MouseRect.Left;
m_MouseRect.Height = p_Point.Y - m_MouseRect.Top;
DrawRectangle();
}
/// <summary>
/// 绘制区域
/// </summary>
private void DrawRectangle()
{
Rectangle _Rect = pictureBox1.RectangleToScreen(m_MouseRect);
ControlPaint.DrawReversibleFrame(_Rect, Color.White, FrameStyle.Dashed);
}
/// <summary>
/// 开始绘制 并且设置鼠标区域
/// </summary>
/// <param name="StartPoint">开始位置</param>
private void DrawStart(Point p_Point)
{
Cursor.Clip = pictureBox1.RectangleToScreen(new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
m_MouseRect = new Rectangle(p_Point.X, p_Point.Y, 0, 0);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
m_MouseIsDown = true;
DrawStart(new Point(e.X, e.Y));
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (m_MouseIsDown) ResizeToRectangle(new Point(e.X, e.Y));
}
float w = 0;//宽度比例
float h = 0;//高度比例
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
w = (float)intwidth / (float)pictureBox1.Width;
h = (float)intheigh / (float)pictureBox1.Height;
Cursor.Clip = Rectangle.Empty;
m_MouseIsDown = false;
DrawRectangle();
if (m_MouseRect.X == 0 || m_MouseRect.Y == 0 || m_MouseRect.Width == 0 || m_MouseRect.Height == 0)
{
//如果区域没0 就不执行委托
}
else
{
if (SetRectangel != null) SetRectangel(pictureBox1, m_MouseRect);
}
ControlPaint.DrawReversibleFrame(pictureBox1.RectangleToScreen(m_MouseRect), Color.Red, FrameStyle.Thick);
System.Drawing.Bitmap bmp = new Bitmap(pictureBox1.Image);
Graphics m_mouse = Graphics.FromImage(bmp);
Brush brush = new SolidBrush(Color.Red);
Pen pen = new Pen(brush, 3);
int wd = (int)(w * m_MouseRect.Width);
int hd = (int)(h * m_MouseRect.Height);
m_mouse.DrawEllipse(pen, new Rectangle((int)(m_MouseRect.X * w), (int)(h * m_MouseRect.Y), wd, hd));
m_mouse.DrawRectangle(pen, new Rectangle((int)(m_MouseRect.X * w), (int)(h * m_MouseRect.Y), wd, hd));
//MemoryStream ms = new MemoryStream();
// bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
bmp.Save("D://1.bmp");
pictureBox3.Image = bmp;
//截屏可以实现效果
Bitmap myImage = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(myImage);
g.CopyFromScreen(pictureBox1.PointToScreen(pictureBox1.Location), new Point(0, 0), pictureBox1.PreferredSize);
IntPtr dc1 = g.GetHdc();
g.ReleaseHdc(dc1);
pictureBox2.Image = (Image)myImage;
}
/// <summary>
/// 刷新绘制
/// </summary>
/// <param name="p"></param>
private void ResizeToRectangle(Point p_Point)
{
DrawRectangle();
m_MouseRect.Width = p_Point.X - m_MouseRect.Left;
m_MouseRect.Height = p_Point.Y - m_MouseRect.Top;
DrawRectangle();
}
/// <summary>
/// 绘制区域
/// </summary>
private void DrawRectangle()
{
Rectangle _Rect = pictureBox1.RectangleToScreen(m_MouseRect);
ControlPaint.DrawReversibleFrame(_Rect, Color.White, FrameStyle.Dashed);
}
/// <summary>
/// 开始绘制 并且设置鼠标区域
/// </summary>
/// <param name="StartPoint">开始位置</param>
private void DrawStart(Point p_Point)
{
Cursor.Clip = pictureBox1.RectangleToScreen(new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
m_MouseRect = new Rectangle(p_Point.X, p_Point.Y, 0, 0);
}