C#中PictureBox控件中获取鼠标相对图片位置的坐标

yxfox 2009-07-22 03:53:14
我在PictureBox中显示了一张大图片,该图片为2202*1900分辨率,PictureBox大小为800*600,该图片可自由移动。
我想获得鼠标在图片上点击时相对与图片上的坐标应如何操作?是相对于图片,不是控件。谢谢。
...全文
7054 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
geminizane 2010-08-02
  • 打赏
  • 举报
回复
mark下...那个代码看得内牛满面
..
mcdjxiao 2010-02-23
  • 打赏
  • 举报
回复
a a a aaaaaaa
xue12300 2009-12-15
  • 打赏
  • 举报
回复
C#中图片最大支持多大?
北京的雾霾天 2009-07-23
  • 打赏
  • 举报
回复
做了一个测试,参考代码:

public partial class Form2 : Form
{
private Point m_ImgDrawPoint;
private Point m_ImgTmpPoint;
private Point m_MouseDownPoint;
private Image m_Image;
private bool m_MouseInImage;

public Form2()
{
InitializeComponent();
this.DoubleBuffered = true;
}
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
OpenFileDialog of = new OpenFileDialog();
of.Filter = "jpg文件;asdfsad;asdfsa|*.jpg|bmp文件|*.bmp|gif文件|*.gif";
if (of.ShowDialog(this) == DialogResult.OK)
{
m_ImgDrawPoint = Point.Empty;
if (this.m_Image != null)
{
this.m_Image.Dispose();
}
m_Image = Image.FromFile(of.FileName);
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.m_Image != null)
{
e.Graphics.DrawImage(this.m_Image, this.m_ImgDrawPoint.X, this.m_ImgDrawPoint.Y, this.m_Image.Width, this.m_Image.Height);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
if (this.m_Image != null)
{
m_MouseDownPoint = e.Location;
this.m_ImgTmpPoint = this.m_ImgDrawPoint;
Rectangle rect = new Rectangle(this.m_ImgDrawPoint.X, this.m_ImgDrawPoint.Y, this.m_Image.Width, this.m_Image.Height);
m_MouseInImage = rect.Contains(e.Location);
if (m_MouseInImage)
{
Point msPoint = e.Location;
msPoint.Offset(-this.m_ImgDrawPoint.X, -this.m_ImgDrawPoint.Y);
this.Text = string.Format("鼠标在图片上的位置:{0}", msPoint.ToString());
}
}
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
if (m_MouseInImage)
{
Point pt = this.m_ImgTmpPoint;
pt.Offset(e.X - this.m_MouseDownPoint.X, e.Y - this.m_MouseDownPoint.Y);
this.m_ImgDrawPoint = pt;

this.Invalidate();
}
}
}
}
北京的雾霾天 2009-07-23
  • 打赏
  • 举报
回复
对当前鼠标位置使用图片移动后的位置做Offset。也就是你要知道当前图片的位置才行,

Point pt=e.Location.Offset(ptImage);
ConanKid 2009-07-23
  • 打赏
  • 举报
回复
那你可以把picturebox放在窗口控件当中,比如panel,你移动的时候不要移动图片,而移动picturebox控件.这样picturebox相对于panel的位置也有,想取什么位置都可以做到.
yxfox 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 conankid 的回复:]
引用楼主 yxfox 的回复:
我在PictureBox中显示了一张大图片,该图片为2202*1900分辨率,PictureBox大小为800*600,该图片可自由移动。
我想获得鼠标在图片上点击时相对与图片上的坐标应如何操作?是相对于图片,不是控件。谢谢。


不知道你这里的自由移动是如何实现的?如果是把picturebox放在一个容器控件(比如panel)当中,那么就可以根据panel与picturebox的相对位置关系,取出想要的位置.
[/Quote]

我只用了一个PictureBox控件,然后使用事件对PictureBox1.Image进行拖动。
yxfox 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 conankid 的回复:]
那你可以把picturebox放在窗口控件当中,比如panel,你移动的时候不要移动图片,而移动picturebox控件.这样picturebox相对于panel的位置也有,想取什么位置都可以做到.
[/Quote]

感谢提醒,已解决。
ConanKid 2009-07-22
  • 打赏
  • 举报
回复
[Quote=引用楼主 yxfox 的回复:]
我在PictureBox中显示了一张大图片,该图片为2202*1900分辨率,PictureBox大小为800*600,该图片可自由移动。
我想获得鼠标在图片上点击时相对与图片上的坐标应如何操作?是相对于图片,不是控件。谢谢。
[/Quote]

不知道你这里的自由移动是如何实现的?如果是把picturebox放在一个容器控件(比如panel)当中,那么就可以根据panel与picturebox的相对位置关系,取出想要的位置.
meslog 2009-07-22
  • 打赏
  • 举报
回复
顶一下。

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧