1
Answer

My image didnot move smoothly, please help me.

k

k

16y
5.5k
1
I try to make small image movable, but it didnot move smoothly. Anyone can help me?
Thanks in advance.
My project: http://www.adrive.com/public/cb65f291603e8...2ce7200841.html
namespace ImageMoving
{
  public class car
    {
        public Image pic;
        public int x;
        public int y;
        public car()
        {
               pic = Image.FromFile("myimage.jpg");
        }

    }
    public partial class Form1 : Form
    {
        
        Timer MyTimer = new Timer();
        car MyCar = new car();
        Image imgbg;
        public Form1()
        {          
            InitializeComponent();
            imgbg = Image.FromFile("background.jpg");
            MyCar.x = 0;
            MyCar.y = 0;
            MyTimer.Interval = 20;
            MyTimer.Start();
            MyTimer.Tick += new EventHandler(MyTimer_Tick);            
        }

        void MyTimer_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }
        
        protected override void OnPaint(PaintEventArgs e)
        {
            Image imgBuffer = new Bitmap(this.Width, this.Height);
            Graphics grBuffer = Graphics.FromImage(imgBuffer);
            grBuffer.DrawImage(imgbg, 0, 0);
            using (grBuffer)
            {
                grBuffer.DrawImage(MyCar.pic, MyCar.x, MyCar.y);
                MyCar.x += 5;
                e.Graphics.DrawImage(imgBuffer, 0, 0);
            }
            imgBuffer.Dispose();
        }

    }
}
Answers (1)