Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have picture of mouse , and one is a background image which contains a grid( 20X20). i have a thread which move the position of mouse picture on the background image and then DrawCircle display that image in the picturebox.

now i want to create more than one thread and move more than one mouse images on the same background.For one mouse it is okay. but for 2 or more its looks like that mouse are blinking.
while they move but the movement is not look like realistic.

following is the code of DrawCircle. where p is the point where i want to display, r is the image of mouse to be displayed in picturebox with the name of pbox and bkmg is the bckground image on which all the mouses's images should be display.

any idea or solution please.
if it is not possible or not a good idea so anyone could please give me a reasonable idea for the solution other than picture box.

C#
private delegate void DrawCircleDelegate(Point pp, Bitmap r, PictureBox  pbox,Bitmap bkmg);
      public void DrawCircle(Point p, Bitmap ratImage, PictureBox pbox, Bitmap bkmg)
      {

          if (InvokeRequired)
          {
              DrawCircleDelegate drwCircle = new DrawCircleDelegate((pp, r, q,s) => DrawCircle(p, ratImage, pbox,bkmg));
              this.BeginInvoke(drwCircle, new object[] { p, ratImage, pbox,bkmg });
          }


          lock (_locker)
          {



              Bitmap tempbitmap = (Bitmap)bkmg.Clone ();
              Graphics g = Graphics.FromImage(tempbitmap);

              if (ratImage == null)
                  g.FillEllipse(Brushes.Green, p.X - 15, p.Y - 15, 30, 30);
              else
                  g.DrawImage(ratImage, p.X - 15, p.Y - 15, 30, 30);


              pbox.Image = tempbitmap;


          }

      }
Posted
Comments
Sergey Alexandrovich Kryukov 3-Feb-14 11:30am    
Why PictureBox?.. :-)
—SA

Don't draw anything on a PictureBox. Of course, you can do it, but it makes no sense at all. This control does not help you to draw anything dynamic, interactive or animated, will only present some extra hassles and waste some extra resources, first of all, your working time. The target for drawing is either the image itself (Bitmap) or screen or other context.

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

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

—SA
 
Share this answer
 
Comments
Muhammad Tufail 1979 3-Feb-14 12:51pm    
of course picturebox is problematic but could you please recommend for my problem , the control panel?
i am using a bitmap and drawing through graphics then put it into the picturebox.
i dont know how to do it with control panel please.
Sergey Alexandrovich Kryukov 3-Feb-14 15:02pm    
It's not "problematic", it's redundant. Can you see the difference? Bitmap is unrelated to PictureBox. Moreover, PictureBox is not really needed in .NET, this is something one could easily add to code. But not Image or Bitmap. All you need it just to forget about the control, unless you need static images. (UI/Graphics is not composed of controls.)
"How to do wit control panel"? What is control panel. Rendering is done on System.Drawing.Control. How? I described in detail in my past answers. Just read them. If something is not clear, ask some follow-up questions, I'll gladly answer.
—SA
Muhammad Tufail 1979 4-Feb-14 10:30am    
could you please solve this problem.
cross-thread operation not valid: Control " accessed from a thread other than the thread it was created
Sergey Alexandrovich Kryukov 4-Feb-14 10:55am    
I will, but are you going to accept this answer formally?
—SA
skydger 4-Feb-14 13:55pm    
+5 :)
hi every one,

i have a method in which i want to display an image in the panel control.
first i put this picture in the picturebox then i add it to the panel control.

i am using two threads but it give me the error as i have mentioned in the subjection of this question.

error i am getting at line
this.agentbox.left = p.x;

anybody could give me suggestion please.

C#
private delegate void DrawOnPanelCircleDelegate(Point pp, Bitmap r );
       public void DrawOnPanelCircle(Point p, Bitmap ratImage)
       {

           if (InvokeRequired)
           {
               DrawOnPanelCircleDelegate drwOnPanelCircle = new DrawOnPanelCircleDelegate((pp, r) => DrawOnPanelCircle(p, ratImage));
               this.BeginInvoke(drwOnPanelCircle, new object[] { p, ratImage });
           }


           lock (_locker)
           {
               this.agentBox .Image = ratImage;
               this.agentBox.SizeMode = PictureBoxSizeMode.StretchImage;

               //this.Invoke((MethodInvoker)delegate
               //{
                   this.agentBox.Left = p.X;
                   this.agentBox.Top = p.Y;
               //});  
               
              
              // Application.DoEvents();


           }



cross-thread operation not valid: Control " accessed from a thread other than the thread it was created
 
Share this answer
 
Muhammad Tufail wrote:
could you please solve this problem. cross-thread operation not valid: Control " accessed from a thread other than the thread it was created
Sure. This is solved by using UI thread invocation mechanism.

You cannot call anything related to UI from non-UI thread (well, almost). Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900