mover una imagen sobre otra imagen

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace d3
    public partial class Form1 : Form
        public Form1()
            pictureBox1.Image = Image.FromFile("C:\\Users\\ocean\\Desktop\\deneme.png");
            pictureBox1.Location = new Point(0, 0);
            pictureBox2.Image = Image.FromFile("C:\\Users\\ocean\\Desktop\\pul1.png");

        protected override void OnMouseClick(MouseEventArgs e)
            Graphics theGraphics = Graphics.FromHwnd(this.Handle);

            for (int i = 0; i < 200; i += 5)
                pictureBox2.Location = new Point(i, 100);


In this code picturebox2 is moving ok but previous locations image stay ON THEuntil the loop finishes. When loop finished the older parts would be erased. I dont want the previous paintings inside the loop I just want to move on pictureBox1. Im new at C# so please help me:) In J2Me I was using flushgraphics but here I tried and it did not work and If you can give an example I would be happy .

preguntado el 27 de agosto de 11 a las 15:08

do not do this in a for loop with a thread.sleep, instead do it with a timer that moves the image 5 pixels every 50 ms then stops. This will allow the change to draw to the screen properly -

This code doesn't allow the form's PaintBackground method to run, required to replace the pixels. Looping like this is never not a problem, stuff stops working properly if the message loop doesn't run. Use a Timer instead, this.Update() is the band-aid. -

1 Respuestas

In C#, just as in Swing, if you are on the UI or event thread, nothing that you change will be noticed by the user until you are done.

So, if you want to move these, your best bet is to start by getting off the UI thread, by starting a new thread, and then go through your loop.

But, the problem is that you will need to be on the UI thread to make the changes.

This is why your sleep didn't work, you are just putting the event thread to sleep, btw.

Which version of C# are you using? There are many options on creating a thread, and on working with the UI thread.

Here is a link to creating a thread:

Here is a way to deal with how to get back to the UI thread:

For example, to create a thread you can do this, which came from

I do my threads this way as I find it simpler to see what is happening.

 (new Thread(() => {
        MessageBox.Show("Long Running Work Finished!");
    }) { Name = "Long Running Work Thread",
        Priority = ThreadPriority.BelowNormal }).Start();

The most complete answer on how to do your update on the UI thread would be this question:

¿Cómo actualizar la GUI desde otro hilo en C #?


For the part where it states to do long running work, you can add this before the thread block:

TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();

Then you can do this inside your thread:

(new Task(() =>
        //copy pixel
        pictureBox2.Invalidate();  // You may want to just invalidate a small block                 around the pixel being changed, or every some number of changes.

By making these changes you could simplify making the change you want, but some of this may be more complex than what you want, which is why I gave some other links to give more information. But, for more on using Task you can look at this excellent blog:

contestado el 23 de mayo de 17 a las 14:05

visual c#2010 express edition can you write an example code because in j2me I wrote in runnable method which is an interface. Inside it there is a loop while(true) and in the end there is a thread and each movement I wrote flushGraphics and it works well however in c# I tried flushgraphics it does not wok I m new at c# so if you can write an example code it would be grateful. Thnx by the way - albatros

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.