如何在图像上绘制多个图像并通过图片框显示 [英] how to draw more than one images on an image and to display through picturebox

查看:76
本文介绍了如何在图像上绘制多个图像并通过图片框显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有鼠标的图片,一个是包含网格(20X20)的背景图片。我有一个线程,它将鼠标图片的位置移动到背景图像上,然后DrawCircle在图片框中显示该图像。



现在我想创建多个线程并在同一背景上移动多个鼠标图像。对于一个鼠标,它没关系。但对于2个或更多它看起来像鼠标闪烁。

他们移动但运动看起来不太现实。



关注是DrawCircle的代码。其中p是我想要显示的点,r是要在pbox中显示的鼠标图像,其名称为pbox,而bkmg是应该显示所有鼠标图像的bckground图像。



请问任何想法或解决方案。

如果不可能或不是一个好主意所以任何人都可以请你给我一个合理的想法除了图片框之外的解决方案。



 私人 委托  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)
{



位图tempbitmap =(位图)bkmg.Clone();
Graphics g = Graphics.FromImage(tempbitmap);

if (ratImage == null
g.FillEllipse (Brushes.Green,pX - 15 ,pY - 15 30 30 );
else
g.DrawImage(ratImage,pX - 15 ,pY - < span class =code-digit> 15 , 30 30 );


pbox.Image = tempbitmap;


}

}

解决方案

不要在 PictureBox 上绘制任何内容。当然,你可以做到,但根本没有任何意义。此控件无法帮助您绘制动态,交互式或动画的任何内容,只会带来一些额外的麻烦并浪费一些额外的资源,首先是您的工作时间。绘图的目标是图像本身(位图)或屏幕或其他上下文。



请参阅我的过去的回答:

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

在C#中绘制一个矩形 [ ^ ],

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


关于图像渲染,请参阅:

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

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

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

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

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



-SA


嗨每一个,



i有一个我想要的方法在面板控件中显示图像。

首先我把这张图片放在图片框中然后我将它添加到面板控件中。



i am使用两个线程,但它给出了我在这个问题的提交中提到的错误。



错误我在线上

this.agentbox.left = px;



任何人都可以给我建议。



  private  委托  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)委托
// {
.agentBox.Left = pX;
this .agentBox.Top = pY;
// });


// Application.DoEvents();


}







跨线程操作无效:控制从创建它以外的线程访问


Muhammad Tufail写道:

你可以解决这个问题。跨线程操作无效:控制从创建它的线程以外的线程访问

确定。这是通过使用UI线程调用机制解决的。



你不能从非UI线程调用与UI相关的任何东西(好吧,几乎)。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA


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.

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;


          }

      }

解决方案

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


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.

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


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


这篇关于如何在图像上绘制多个图像并通过图片框显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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