如何在图片框中的图片上移动圆圈 [英] how to move circle on a picture in picturebox

查看:165
本文介绍了如何在图片框中的图片上移动圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想在图片上创建一个圆圈,然后在图片框中显示。我在图片上绘制了一个10乘10(10行和10列)的网格。然后我在该网格中选择了一个起始单元格,比如说56(当前单元格),并选择下一个单元格(100)。现在我想逐渐将单元格从56移动到100。当它达到100然后程序自动选择下一个单元格说30.现在它必须从100移动到30.

我确实尝试使用计时器移动它但我没有获得成功。

Hi All,

i want to create a circle on the picture please and then to show in the picturebox. i have drawn a grid of 10 by 10 ( 10 rows and 10 col) on the picture. then i have selected a start cell in that grid, say 56, (which is current cell) and also select next cell say ( 100). now i want to move circle from cell 56 to 100 gradually. when it reached to 100 then program automatically select next cell say 30. now it have to move from 100 to 30.
i did try to move it using timer but i didn't get success.

private void SimTimer_Tick(object sender, EventArgs e)
       {
           pictureBox1.Image = testImage;
           nxtno = utilObject.SelectNextCell(startCell, rnd);
           startCell = nxtno;       
           DrawCircle(Get_CellCenterPoint(nxtno));

       }



通过这个代码我只是在单元格中单独绘制,但我想逐渐移动它请任何有一些想法请。

或一段代码??


through this code i just drawcircle individually in cell, but i want to move it gradually please any have some idea please.
or piece of code??

i did try like this, but it run only once, how to set the timer for infinite please.
if (startCell != nxtno)
                {
                    moveCircle();
                }
 private void moveCircle()
        {
             prvCellCenter = new Point(myGrid.Cells[startCell].CornerPoints[0].X + 22,
                                            myGrid.Cells[startCell].CornerPoints[0].Y + 21);

             curntCellCenter = new Point(myGrid.Cells[nxtno].CornerPoints[0].X + 22,
                                                myGrid.Cells[nxtno].CornerPoints[0].Y + 21);

             moveTimer.Start();


        }
 private void moveTimer_Tick(object sender, EventArgs e)
        {

            if (startCell < nxtno)
            {

                if (prvCellCenter.X < curntCellCenter.X && prvCellCenter.Y < curntCellCenter.Y)
                {
                    prvCellCenter.X += 5;
                    prvCellCenter.Y += 5;
                }
                else if (prvCellCenter.X < curntCellCenter.X) prvCellCenter.X += 5;
                else if (prvCellCenter.Y < curntCellCenter.Y) prvCellCenter.Y += 5;
                DrawCircle(prvCellCenter);
            }

            else if (startCell > nxtno)
            {

                if (prvCellCenter.X > curntCellCenter.X && prvCellCenter.Y > curntCellCenter.Y)
                {
                    prvCellCenter.X -= 5;
                    prvCellCenter.Y -= 5;
                }
                else if (prvCellCenter.X < curntCellCenter.X) prvCellCenter.X -= 5;
                else if (prvCellCenter.Y < curntCellCenter.Y) prvCellCenter.Y -= 5;
                DrawCircle(prvCellCenter);


            }
        }







谢谢




thanks

推荐答案

您的主要问题是使用 PictureBox 。我完全没有意义。这个控件,一个图像和父控件之间的中间人,根本没有帮助,只使用额外的资源并占用你的开发时间。在你的情况下,它没有任何用处;因为它是为完全不同的目的而设计的:作为显示静态(或几乎总是静态)图像的快捷方式。



怎么办?请查看我过去的答案:

在其中附加图片图片框 [ ^ ],

画一个矩形C# [ ^ ],

如何从旧图纸中清除面板 [ ^ ]。



另见过去的答案:

在面板上捕获图纸 [ ^ ],

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

如何加速我的vb.net应用程序? [ ^ ],

mdi子表单之间的绘制线 [ ^ ],

用C#.net鼠标滚轮缩放图像 [ ^ ]。



我希望你有这个想法。



-SA
Your main problem is using PictureBox. I makes no sense at all. This control, a middleman between an image and the parent control, does not help you at all, only uses extra resources and eats up your development time. In your case, it does not anything useful; as it is designed for completely different purpose: as a shortcut to showing static (or almost always static) images.

What to do? Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

See also these past answers:
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^],
Drawing Lines between mdi child forms[^],
Zoom image in C# .net mouse wheel[^].

I hope you got the idea.

—SA


你需要建立什么条件您希望将圆圈重置为其原始位置,或者您希望使圆圈沿着用于从原点移动到目的地的相同路径移回原点的条件。



当您可以精确定义该条件时,然后在Timer的Click EventHandler中测试它,然后做正确的事,或许可以将X和Y上的移动增量从正变为负。



根据你的目标,你也可以考虑:



1.把网格放在一个PictureBox

2.在另一个PictureBox中使用透明度放置一个圆形的.png图形文件,并将第二个PictureBox移动到保存网格绘图的第一个PictureBox的顶部(w这不需要是透明的。



......或者......



1.你可以使用C#中的Visual Basic Power Pack绘制网格和圆圈中的任何一个或两个。



这里有一个帖子,我给出了一个使用C#中的VB PowerPack:[ ^ ]。



这两种技术都可以让你操纵对象,而不是在Paint事件中创建栅格(位图)。使用Paint Event你必须不断地绘制和重新绘制网格和圆形,但是......取决于你绘制的内容......你可能有更好的动画视觉分辨率。
You need to establish what condition you wish to reset the circle to its original position, or what condition you wish to cause the circle to move back to its origin following the same path it used to move from origin to destination.

When you can precisely define that condition, then test for it in your Timer's Click EventHandler, and then "do the right thing," perhaps changing the increment of moves on X, and Y, axes from positive to negative.

Depending on your goals, you could also consider:

1. put the grid in one PictureBox
2. put a .png graphic file of a circle using transparency in another PictureBox, and move the second PictureBox on top of the first PictureBox that holds the drawing of the grid (which need not be transparent).

... or ...

1. You could use the Visual Basic Power Pack in C# to draw either, or both, of grid, and circle.

There's a post here where I give a specific example of using the VB PowerPack in C#: [^].

Both these techniques would let you manipulate "objects" rather deal with creating raster (bitmaps) in the Paint Event. Using the Paint Event you've got to continually draw, and re-draw, the grid and the circle, but ... depending on what you are drawing ... you might have better visual resolution of an animation.


这篇关于如何在图片框中的图片上移动圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆