PictureBox 相对于窗体的绘图坐标
我在表单上有一个图片框。
在窗体的 Load 事件中,我创建图形如下:
imageGraphics = Graphics.FromImage(PictureBox1.Image)
然后,在 PictureBox_MouseMove 事件中,我绘制椭圆:
imageGraphics.FillEllipse(New SolidBrush(brushColor), e.X, e.Y, brushWidth, brushWidth)
无论我尝试什么,它总是在错误的坐标上绘制。我尝试了 e.Location.PointToClient()、PointToScreen() 和 Cursor.Position。一切都与预期相去甚远(我需要准确地绘制光标所在的位置)。
每当调整表单大小(PictureBox 也一样,因为它的 Anchor 属性设置为展开),绘图与光标的相对位置就会发生变化。
我有什么遗漏的吗?
I have a PictureBox on a form.
In Load event of the form I create graphics as follows:
imageGraphics = Graphics.FromImage(PictureBox1.Image)
Then, in PictureBox_MouseMove event I draw ellipse:
imageGraphics.FillEllipse(New SolidBrush(brushColor), e.X, e.Y, brushWidth, brushWidth)
No matter what I try, it always draws on incorrect coordinates. I tried e.Location.PointToClient(), PointToScreen(), and Cursor.Position. All far from expected (I need to draw exactly where the cursor is).
Whenever form is resized (and PictureBox, too, as it's Anchor property is set to expand), relative position of drawing to cursor changes.
Is there anything I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来很可疑,就像你的 pictureBox 上的 sizeMode 不正确。尝试使 PictureBox 图像的大小与 PictureBox 的大小相同。
This sounds suspiciously like an incorrect sizeMode on your pictureBox. Try making the size of the PictureBox image the same as that of the PictureBox.
虽然这是 1.5 年前的事情,但获取相对于 PictureBox 的坐标的正确调用是:
我想,它对将来的人会有用。
Though this is 1.5 years old, the correct call to get the coordinates relative to PictureBox is:
I guess, it will useful for someone in the future.