开发者

PictureBox Drawing Coordinates Relative to Form

开发者 https://www.devze.com 2023-01-08 07:51 出处:网络
I have a PictureBox on a form. In Load event of the form I create graphics as follows: imageGraphics = Graphics.FromImage(PictureBox1.Image)

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 Curs开发者_运维知识库or.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?


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.


Though this is 1.5 years old, the correct call to get the coordinates relative to PictureBox is:

   Dim p1 as point=PictureBox1.PointToClient(Windows.Forms.Cursor.Position)
   imageGraphics.FillEllipse(New SolidBrush(brushColor), p1.X, p1.Y, brushWidth, brushWidth)

I guess, it will useful for someone in the future.

0

精彩评论

暂无评论...
验证码 换一张
取 消