不重叠也好说,但是要保证每一个图片都大小一致,否则坐标不好计算,出来的效果可能不尽人意,位置显示乱,如果大小一致按行列排好就是了,只需要在加载的时候设置好位置。画线倒简单了,可以参考下面的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Drawing.Imaging;namespace DragImages { public partial class Form1 : Form { public class ImgInfo { private Image m_Img; private Point m_Pot; public Image Img { get { return m_Img; } } public Point Pot { get { return m_Pot; } set { m_Pot = value; } } public ImgInfo(Image img) { m_Img = img; } } private List<ImgInfo> imgList; private Point lastPoint; private Point lastMsPoint; private ImgInfo curImgInfo; public Form1() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.imgList = new List<ImgInfo>(); this.DoubleBuffered = true; } protected override void OnDoubleClick(EventArgs e) { base.OnDoubleClick(e); OpenFileDialog of = new OpenFileDialog(); of.Multiselect = true; of.Filter = "jpg文件;asdfsad;asdfsa|*.jpg|bmp文件|*.bmp|gif文件|*.gif"; if (of.ShowDialog(this) == DialogResult.OK) { imgList.Clear(); for (int i = 0; i < of.FileNames.Length; i++) { Image img = Image.FromFile(of.FileNames[i]); imgList.Add(new ImgInfo(img)); } } if (this.imgList.Count > 0) { this.Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e);
Point m_beginPoint = Point.Empty; for (int i = 0; i < this.imgList.Count; i++) { Rectangle rect = new Rectangle(this.imgList[i].Pot, this.imgList[i].Img.Size); e.Graphics.DrawImage(this.imgList[i].Img,rect); Point m_endPoint = new Point(rect.Left / 2 + rect.Right / 2, rect.Top / 2 + rect.Bottom / 2); if (m_beginPoint != Point.Empty) { e.Graphics.DrawLine(SystemPens.ControlDarkDark, m_beginPoint, m_endPoint); } m_beginPoint = m_endPoint; } } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); this.lastMsPoint = e.Location; for (int i = this.imgList.Count - 1; i > -1; i--) { ImgInfo info = this.imgList[i]; Rectangle rect = new Rectangle(info.Pot, info.Img.Size); if (rect.Contains(e.Location)) { this.curImgInfo = info; this.lastPoint = info.Pot; return; } } } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (this.Capture && this.curImgInfo != null) { this.curImgInfo.Pot = new Point(this.lastPoint.X + e.X - this.lastMsPoint.X, this.lastPoint.Y + e.Y - this.lastMsPoint.Y); this.Invalidate(); } } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); this.curImgInfo = null; } } }
因此是不是pictureBox也就无所谓了。既然不一定是pictureBox,那么也就没有必要考虑使用pictureBox自身的显示Image的功能了,可以考虑使用在基类的Onpaint方法里来处理。我作了个测试的代码可以参考一下:
public partial class Form1 : Form
{
public class ImgInfo
{
private Image m_Img;
private Point m_Pot; public Image Img
{
get { return m_Img; }
}
public Point Pot
{
get { return m_Pot; }
set { m_Pot = value; }
}
public ImgInfo(Image img)
{
m_Img = img;
}
}
private List<ImgInfo> imgList;
private Point lastPoint;
private Point lastMsPoint;
private ImgInfo curImgInfo;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.imgList = new List<ImgInfo>();
this.DoubleBuffered = true;
}
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
OpenFileDialog of = new OpenFileDialog();
of.Multiselect = true;
of.Filter = "jpg文件;asdfsad;asdfsa|*.jpg|bmp文件|*.bmp|gif文件|*.gif";
if (of.ShowDialog(this) == DialogResult.OK)
{
imgList.Clear();
for (int i = 0; i < of.FileNames.Length; i++)
{
Image img = Image.FromFile(of.FileNames[i]);
imgList.Add(new ImgInfo(img));
}
}
if (this.imgList.Count > 0)
{
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
for (int i = 0; i < this.imgList.Count; i++)
{
e.Graphics.DrawImage(this.imgList[i].Img, new Rectangle(this.imgList[i].Pot, this.imgList[i].Img.Size));
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.lastMsPoint = e.Location;
for (int i = this.imgList.Count - 1; i > -1; i--)
{
ImgInfo info = this.imgList[i];
Rectangle rect = new Rectangle(info.Pot, info.Img.Size);
if (rect.Contains(e.Location))
{
this.curImgInfo = info;
this.lastPoint = info.Pot;
return;
}
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (this.Capture && this.curImgInfo != null)
{
this.curImgInfo.Pot = new Point(this.lastPoint.X + e.X - this.lastMsPoint.X, this.lastPoint.Y + e.Y - this.lastMsPoint.Y);
this.Invalidate();
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.curImgInfo = null;
}
}
再在pictureBox 的mouseup, mousedown, mousemove 事件里处理移动.
你可以建立一个新的项目,把代码放到里面运行。如果你是VS2003,可以考虑把List<ImgInfo> 换成ArrayList并对代码做一些小的改动就可以了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;namespace DragImages
{
public partial class Form1 : Form
{
public class ImgInfo
{
private Image m_Img;
private Point m_Pot; public Image Img
{
get { return m_Img; }
}
public Point Pot
{
get { return m_Pot; }
set { m_Pot = value; }
}
public ImgInfo(Image img)
{
m_Img = img;
}
}
private List<ImgInfo> imgList;
private Point lastPoint;
private Point lastMsPoint;
private ImgInfo curImgInfo;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.imgList = new List<ImgInfo>();
this.DoubleBuffered = true;
}
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
OpenFileDialog of = new OpenFileDialog();
of.Multiselect = true;
of.Filter = "jpg文件;asdfsad;asdfsa|*.jpg|bmp文件|*.bmp|gif文件|*.gif";
if (of.ShowDialog(this) == DialogResult.OK)
{
imgList.Clear();
for (int i = 0; i < of.FileNames.Length; i++)
{
Image img = Image.FromFile(of.FileNames[i]);
imgList.Add(new ImgInfo(img));
}
}
if (this.imgList.Count > 0)
{
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Point m_beginPoint = Point.Empty; for (int i = 0; i < this.imgList.Count; i++)
{
Rectangle rect = new Rectangle(this.imgList[i].Pot, this.imgList[i].Img.Size);
e.Graphics.DrawImage(this.imgList[i].Img,rect);
Point m_endPoint = new Point(rect.Left / 2 + rect.Right / 2, rect.Top / 2 + rect.Bottom / 2);
if (m_beginPoint != Point.Empty)
{
e.Graphics.DrawLine(SystemPens.ControlDarkDark, m_beginPoint, m_endPoint);
}
m_beginPoint = m_endPoint;
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.lastMsPoint = e.Location;
for (int i = this.imgList.Count - 1; i > -1; i--)
{
ImgInfo info = this.imgList[i];
Rectangle rect = new Rectangle(info.Pot, info.Img.Size);
if (rect.Contains(e.Location))
{
this.curImgInfo = info;
this.lastPoint = info.Pot;
return;
}
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (this.Capture && this.curImgInfo != null)
{
this.curImgInfo.Pot = new Point(this.lastPoint.X + e.X - this.lastMsPoint.X, this.lastPoint.Y + e.Y - this.lastMsPoint.Y);
this.Invalidate();
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.curImgInfo = null;
}
}
}
帮我看看我的帖子啊
http://topic.csdn.net/u/20080310/10/2f66df37-54af-45d2-88a2-ca5cd24d7586.html?seed=2099754099