picturebox装载图片后,然后在在这个图片上用鼠标按下,再拖动大小,松开后就画了一个框,pictruebox尺寸和图片本身尺寸有关系的,每次画后都要重绘,效果图
直接贴代码:
///
/// 鼠标状态
///
private bool m_MouseIsDown = false;
///
/// 绘制区域
///
private Rectangle m_MouseRect = Rectangle.Empty;
public delegate void SelectRectangel(object sneder, Rectangle e);
public event SelectRectangel SetRectangel;
int intwidth = 0;
int intheigh = 0;
///打开一个图片
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() ==DialogResult.OK )
{
pictureBox1.ImageLocation = openFileDialog1.FileName;
Image pic = Image.FromFile(openFileDialog1.FileName);
intwidth = pic.Width;
intheigh = pic.Height;
pos = pictureBox1.PointToScreen(pictureBox1.Location);
}
}
//鼠标按下
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;
}
///
/// 刷新绘制
///
///
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();
}
///
/// 绘制区域
///
private void DrawRectangle()
{
Rectangle _Rect = pictureBox1.RectangleToScreen(m_MouseRect);
ControlPaint.DrawReversibleFrame(_Rect, Color.White, FrameStyle.Dashed);
}
///
/// 开始绘制 并且设置鼠标区域
///
/// 开始位置
private void DrawStart(Point p_Point)
{
//pos.X -= panel1.Location.X;
//pos.Y -= panel1.Location.Y;
//pictureBox1.Capture = true;
Cursor.Clip = pictureBox1.RectangleToScreen(new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
m_MouseRect = new Rectangle(p_Point.X, p_Point.Y, 0, 0);
}
以上已经测试成功,图片大小尺寸均对,起始里面还有写文字,但是文字区域无法定位,可以一起讨论
补充:
又弄了个在CE上的,这个无法移动是产生虚线,只能手写笔离开的时候才能出现圈圈